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也有一定的要求: 必 ...
随机推荐
- Linux 关机命令
正确的关机流程是:sync –> shutdown/reboot/halt/poweroff sync 将数据由内存同步到硬盘中. shutdown 关机指令.例如你可以运行如下命令关机: sh ...
- 数据表格datagrid内容整理
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- iOS网络推送消息
在iOS项目的appdelegate.m文件中: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOpti ...
- sfliter__except_handler4
sfliter源码在vs08中编译 出现 错误error LNK2019: unresolved external symbol __except_handler4 referenced in fun ...
- Codeforces Round #383 (Div. 1)
A: 题目大意:给出一个有向图(n<=100),每个点的出度都为1,求最小的t,使得任意两点x,y,如果x走t步后能到y,那么y走t步后到x. 题解: 首先每个点应该都在一个环上,否则无解. 对 ...
- google api autocomplete
<input class="flex-item" id="autocomplete" placeholder="address, zip or ...
- 使用wex5得到的一些教训
博主一直都是做web开发,前段时间有个小想法,想给自己做个android小应用(很小,功能特别简单). 了解到可以用js直接做,貌似很简单,选用了wex5(基于codova插件)来直接开发. 最终发现 ...
- Java 找不到主类错误
Eclipse 运行java 程序,突然出现错误:没有或找不到主类. 在网上找了好多办法,都不行. jdk环境配置啊-->这个一般不会出错,因为以前都不会出现这种问题. 查看项目配置啥的--&g ...
- linux下如何使用sftp命令
sftp 是一个交互式文件传输程式.它类似于 ftp, 但它进行加密传输,比FTP有更高的安全性.下边就简单介绍一下如何远程连接主机,进行文件的上传和下载,以及一些相关操作. 举例,如远程主机的 IP ...
- Disable testSuite and testCase on some environment
def testEnv = context.expand('${#Project#testEnv}') String[] testCases = ["CheckEARouting(ADS)A ...