ASP.NET MVC中多种ActionResult用法总结
最近一段时间做了个ASP.NET MVC4.0的项目,项目马上就要结束了,今天忙里偷闲简单总结一下心得:
1. 如果Action需要有返回值的话,必须是ActionResult的话,可以返回一个EmptyResult,Demo实例如下:
/// <summary>
///
/// </summary>
/// <returns>EmptyResult</returns>
public ActionResult EmptyActionResult()
{
Response.Write("This is a string content");
return new EmptyResult();
}
2. 如果需要转向指定的页面的话,转向一个指定的url,返回类型为RedirectResult,Demo实例如下:
/// <summary>
/// Controller.Redirect(),转向一个指定的url
/// </summary>
/// <returns>RedirectResult</returns>
public ActionResult RedirectResult()
{
return base.Redirect("~/Controllers/DestinationController");
}
3.如果需要转向一个指定的Action,返回类型为RedirectToRouteResult,Demo实例如下:
/// <summary>
/// Controller.Redirect(),转向一个指定的Action
/// </summary>
/// <returns>RedirectToRouteResult</returns>
public ActionResult RedirectToRouteResult()
{
return base.RedirectToAction("DestinationOController");
}
4.如果需要将指定的对象以JSON串的形式返回,返回类型为JsonResult,Demo实例如下:
/// <summary>
/// Controller.Json(),将对象以Json的形式返回
/// </summary>
/// <returns>JsonResult</returns>
public ActionResult GetJsonResult()
{ var jsonObj=new {Name:"Test Name";Age:};
return base.Json(jsonObj);
}
5. 如果需要返回一段Javascript代码,返回类型为JavascriptResult,Demo实例如下:
/// <summary>
/// Controller.JavaScript(),将对象以Javascript脚本的形式返回
/// </summary>
/// <returns>JavaScript</returns>
public ActionResult GetJavascriptResult()
{
return base.JavaScript("alert('This is JavaScript result')");
}
6. 如果需要输出一段指定的内容,返回类型为ContentResult,Demo实例如下:
/// <summary>
/// Controller.Content(),将对象以ContentResult的形式返回
/// </summary>
/// <returns>ContentResult</returns>
public ActionResult ContentResult()
{
string contentString = string.Format("<span style='color: red'>{0}</span>", "This is content result");
return base.Content(contentString);
}
7. 如果需要将一个文件以字节输入的格式输入,返回类型为FileContentResult,Demo实例如下:
/// <summary>
/// Controller.Content(),将对象以byte[]的形式返回
/// </summary>
/// <returns>FileContentResult</returns>
public ActionResult FileContentResult()
{
FileStream fs = new FileStream(Request.PhysicalApplicationPath + "Content/loading.gif", FileMode.Open);
int length = (int)fs.Length;
byte[] buffer = new byte[length];
fs.Read(buffer, , length);
fs.Close();
return base.File(buffer, "image/gif"); }
8. 如果需要将一个文件以文件流的格式输出,返回类型为FileStreamResult,Demo实例如下:
/// <summary>
/// Controller.Content(),将对象以FileStream的形式返回
/// </summary>
/// <returns>FileStreamResult</returns>
public ActionResult FileStreamResult()
{
FileStream fs = new FileStream(Request.PhysicalApplicationPath + "Content/loading.gif", FileMode.Open);
return base.File(fs, @"image/gif");
}
9. 如果需要返回未经授权浏览状态的结果,返回类型为HttpUnauthorizedResult,Demo实例如下:
/// <summary>
/// HttpUnauthorizedResult - 响应给客户端错误代码 401(未经授权浏览状态),
/// 如果程序启用了 Forms 验证,并且客户端没有任何身份票据,则会跳转到指定的登录页
/// </summary>
/// <returns>HttpUnauthorizedResult</returns>
public ActionResult HttpUnauthorizedResult()
{
return new HttpUnauthorizedResult();
}
10. 如果需要返回html页面的部分[.ascx],返回对象的类型为PartialViewResult,Demo实例如下:
/// <summary>
/// Controller.PartialView(),查找PartialView,即 .ascx 文件
/// </summary>
/// <returns>PartialView</returns>
public ActionResult PartialViewResult()
{
return base.PartialView();
}
11. 如果需要返回完整的Html,即完整的View,返回的类型了ViewResult,Demo实例如下:
/// <summary>
/// Controller.View(),查找View,即.aspx 文件
/// </summary>
/// <returns>ViewResult</returns>
public ActionResult ViewResult()
{
// 如果没有指定 View 名称,则寻找与 Action 名称相同的 View
return base.View();
}
12. 使用Get方式调用Action,返回类型为参数为空的ViewResult,Demo程序实例如下:
/// <summary>
/// 用于演示 Get 方式调用 Action
/// id 是根据路由过来的;param1和param2是根据参数过来的
/// </summary>
/// <returns></returns>
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult GetDemo(int id, string param1, string param2)
{
ViewData["ID"] = id;
ViewData["Param1"] = param1;
ViewData["Param2"] = param2; return View();
}
14. 处理上传文件的Action,Demo实例如下:
/// <summary>
/// 处理上传文件的 Action
/// </summary>
/// <param name="file1">与传过来的 file 类型的 input 的 name 相对应</param>
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult UploadFile(HttpPostedFileBase file1)
{
// Request.Files - 获取需要上传的文件。当然,其也会自动映射到同名参数
// HttpPostedFileBase hpfb = Request.Files[0] as HttpPostedFileBase; string targetPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "Upload", Path.GetFileName(file1.FileName));
file1.SaveAs(targetPath); return View("UploadDemo");
}
待完善。。。
ASP.NET MVC中多种ActionResult用法总结的更多相关文章
- Asp.Net MVC中DropDownListFor的用法(转)
2016.03.04 扩展:如果 view中传入的是List<T>类型 怎么使用 DropList 既然是List<T> 那么我转化成 T List<T>的第一个 ...
- Asp.Net MVC中DropDownListFor的用法
在Asp.Net MVC中可以用DropDownListFor的方式来让用户选择已定列表中的一个数值.用法不复杂,这里简单做一个记录. 首先我们要定义一个 Model ,用户在 DropDownLis ...
- 转:Asp.Net MVC中DropDownListFor的用法
在Asp.Net MVC中可以用DropDownListFor的方式来让用户选择已定列表中的一个数值.用法不复杂,这里简单做一个记录. 首先我们要定义一个 Model ,用户在 DropDownLis ...
- 理解ASP.NET MVC中的ActionResult
通常我们在一个ASP.NET MVC项目中创建一个Controller的时候,Index()方法默认的返回类型都是ActionResult,通过查看UML图,ActionResult实际上是一个抽象类 ...
- .NET MVC中的ActionResult
一 摘要 本文介绍了ASP.NET MVC中的ActionResult,本节主要介绍 EmptyResult / Content Result /JavaScriptResult /JsonResu ...
- ASP.NET MVC中Area的另一种用法
ASP.NET MVC中Area的另一种用法 [摘要]本文只是为一行代码而分享 context.MapRoute("API", "api/{controller}/{ac ...
- ASP.NET MVC中常用的ActionResult类型
常见的ActionResult 1.ViewResult 表示一个视图结果,它根据视图模板产生应答内容.对应得Controller方法为View. 2.PartialViewResult 表示一个部分 ...
- .Net Mvc学习——ASP.NET MVC中常用的ActionResult类型
一.定义 MVC中ActionResult是Action的返回结果.ActionResult 有多个派生类,每个子类功能均不同,并不是所有的子类都需要返回视图View,有些直接返回流,有些返回字符串等 ...
- Asp.net MVC 中Controller返回值类型ActionResult
[Asp.net MVC中Controller返回值类型] 在mvc中所有的controller类都必须使用"Controller"后缀来命名并且对Action也有一定的要求: 必 ...
随机推荐
- LTE Air interface Channels-----http://www.rfwireless-world.com/Tutorials/LTE-logical-transport-physical-channels.html
LTE technology works based on three channel types viz. logical channel,transport channel and physica ...
- MindManger学习技巧
ctrl+shift+f 字体颜色
- Laravel框架 mysql 数据库 —— 基本使用
增删改查 配置完数据库连接,就可以使用DB类进行查询了. 查询 $results = DB::select('select * from users where id = ?', array(1)); ...
- sql之多表连接
最近遇到特别多多表连接的问题,因此随笔记下,开始学java和mysql的时间太短,有见解不周的地方,希望读者可以提出探讨. 对于left join.right join和inner join(join ...
- Microsoft Azure Project Oxford 体验
2015年4月29日,微软在Build 2015大会上发布了一个震撼人心的项目: Project Oxford, 可以帮助直接实现图像理解.人脸识别.语音识别.语音合成等功能.虽然说这是号称研究院的项 ...
- Switch图形练习
//package IfAndSwitchs;import java.util.Scanner; public class Mianji { public static void main(Strin ...
- js中,全局变量与直接添加在window属性的区别
在js中定义的全局变量是挂在window下的,而window的属性也一样,那么这两者有什么区别呢? 其实这两者还是有小小的区别的,全局变量是不能通过delete操作符删除的,而直接定义在window上 ...
- Result Maps collection does not contain value for java.lang.Integer异常处理
使用Mybatis的时候出现这个问题是因为配置文件的问题造成的,mybatis需要写大量的配置文件, 尽管有mybatis-generator,但是里面的内容有很多还是要自己去写的,在这过程中难免会出 ...
- 解决eclipse中自带的maven搜索不到非本地第三方包问题
解决eclipse中自带的maven搜索不到非本地第三方包问题 版权声明:本文为博主原创文章,未经博主允许不得转载. 最近使用eclipse中的maven插件时发现,在pom.xml文件中添加第 ...
- as3 代码加解密
private var loader:URLLoader; ... private function init():void { loader = new URLLoader; req=URLRequ ...