常见的ActionResult

1、ViewResult

表示一个视图结果,它根据视图模板产生应答内容。对应得Controller方法为View。

2、PartialViewResult

表示一个部分视图结果,与ViewResult本质上一致,只是部分视图不支持母版,对应于ASP.NET,ViewResult相当于一个Page,而PartialViewResult 则相当于一个UserControl。它对应得Controller方法的PartialView.

3、RedirectResult

表示一个连接跳转,相当于ASP.NET中的Response.Redirect方法,对应得Controller方法为Redirect。

4、RedirectToRouteResult

同样表示一个跳转,MVC会根据我们指定的路由名称或路由信息(RouteValueDictionary)来生成Url地址,然后调用Response.Redirect跳转。对应的Controller方法为RedirectToAction和RedirectToRoute.

5、ContentResult

返回简单的纯文本内容,可通过ContentType属性指定应答文档类型,通过ContentEncoding属性指定应答文档的字符编码。可通过Controller类中的Content方法便捷地返回ContentResult对象。如果控制器方法返回非ActionResult对象,MVc将简单地以返回对象的toString()内容为基础产生一个ContentResult对象。

6、EmptyResult

返回一个空的结果,如果控制器方法返回一个null ,MVC将其转换成EmptyResult对象。

7、JavaScriptResult

本质上是一个文本内容,只是将Response.ContentType设置为application/x-javascript,此结果应该和MicrosoftMvcAjax.js脚本配合使用,客户端接收到Ajax应答后,将判断Response.ContentType的值,如果是application/x-javascript,则直接eval 执行返回的应答内容,此结果类型对应得Controller方法为JavaScript.

8、JsonResult

表示一个Json结果。MVC将Response.ContentType 设置为application/json,并通过JavaScriptSerializer类指定对象序列化为Json表示方式。需要注意,默认情况下,Mvc不允许GET请求返回Json结果,要解除此限制,在生成JsonResult对象时,将其JsonRequestBehavior属性设置为JsonRequestBehavior.AllowGet,此结果对应Controller方法的Json.

9、FileResult(FilePathResult、FileContentResult、FileStreamResult)

这三个类继承于FileResult,表示一个文件内容,三者区别在于,FilePath 通过路径传送文件到客户端,FileContent 通过二进制数据的方式,而FileStream 是通过Stream(流)的方式来传送。Controller为这三个文件结果类型提供了一个名为File的重载方法。

FilePathResult: 直接将一个文件发送给客户端

     FileContentResult: 返回byte字节给客户端(比如图片)

     FileStreamResult: 返回流

10、HttpUnauthorizedResult

表示一个未经授权访问的错误,MVC会向客户端发送一个401的应答状态。如果在web.config 中开启了表单验证(authenication mode=”Forms”),则401状态会将Url 转向指定的loginUrl 链接。

11、HttpStatusCodeResult

返回一个服务器的错误信息

12、HttpNoFoundResult

返回一个找不到Action错误信息

// MVC 中的ActionResult是Action的返回结果. ActionResult有多个子类.
// 并不是所有的子类都需要返回视图View.
// ActionResult是一个抽象类, 他定义了唯一的ExecuteResult方法, 参数为一个ControllerContext. // GET: Home
public ActionResult Index()
{
return View();
} /// <summary>
/// ContentResult用法(返回文本)
/// http://localhost:30735/home/ContentResultDemo
/// </summary>
/// <returns>返回文本</returns>
public ActionResult ContentResultDemo()
{
string str = "ContentResultDemo!";
return Content(str);
} /// <summary>
/// EmptyResult的用法(返回空对象)
/// http://localhost:30735/home/EmptyResultDemo
/// </summary>
/// <returns>返回一个空对象</returns>
public ActionResult EmptyResultDemo()
{
return new EmptyResult();
} /// <summary>
/// FileContentResult的用法(返回图片)
/// http://localhost:30735/home/FileContentResultDemo
/// </summary>
/// <returns>显示一个文件内容</returns>
public ActionResult FileContentResultDemo()
{
FileStream fs = new FileStream(Server.MapPath(@"/Images/001.jpg"), FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[Convert.ToInt32(fs.Length)];
fs.Read(buffer, , Convert.ToInt32(fs.Length));
string contentType = "image/jpeg";
return File(buffer, contentType);
} /// <summary>
/// FilePathResult的用法(返回图片)
/// http://localhost:30735/home/FilePathResultDemo/002
/// </summary>
/// <param name="id">图片id</param>
/// <returns>直接将返回一个文件对象</returns>
public FilePathResult FilePathResultDemo(string id)
{
string path = Server.MapPath(@"/Images/" + id + ".jpg");
//定义内容类型(图片)
string contentType = "image/jpeg";
//FilePathResult直接返回file对象
return File(path, contentType);
} /// <summary>
/// FileStreamResult的用法(返回图片)
/// http://localhost:30735/home/FileStreamResultDemo
/// </summary>
/// <returns>返回文件流(图片)</returns>
public ActionResult FileStreamResultDemo()
{
FileStream fs = new FileStream(Server.MapPath(@"/Images/001.jpg"), FileMode.Open, FileAccess.Read);
string contentType = "image/jpeg";
return File(fs, contentType);
} /// <summary>
/// HttpUnauthorizedResult 的用法(抛出401错误)
/// http://localhost:30735/home/HttpUnauthorizedResult
/// </summary>
/// <returns></returns>
public ActionResult HttpUnauthorizedResultDemo()
{
return new HttpUnauthorizedResult();
} /// <summary>
/// HttpStatusCodeResult的方法(返回错误状态信息)
/// http://localhost:30735/home/HttpStatusCodeResult
/// </summary>
/// <returns></returns>
public ActionResult HttpStatusCodeResultDemo()
{
return new HttpStatusCodeResult(, "System Error");
} /// <summary>
/// HttpNotFoundResult的使用方法
/// http://localhost:30735/home/HttpNotFoundResultDemo
/// </summary>
/// <returns></returns>
public ActionResult HttpNotFoundResultDemo()
{
return new HttpNotFoundResult("not found action");
} /// <summary>
/// JavaScriptResult 的用法(返回脚本文件)
/// http://localhost:30735/home/JavaScriptResultDemo
/// </summary>
/// <returns>返回脚本内容</returns>
public ActionResult JavaScriptResultDemo()
{
return JavaScript(@"<script>alert('Test JavaScriptResultDemo!')</script>");
} /// <summary>
/// JsonResult的用法(返回一个json对象)
/// http://localhost:30735/home/JsonResultDemo
/// </summary>
/// <returns>返回一个json对象</returns>
public ActionResult JsonResultDemo()
{
var tempObj = new { Controller = "HomeController", Action = "JsonResultDemo" };
return Json(tempObj);
} /// <summary>
/// RedirectResult的用法(跳转url地址)
/// http://localhost:30735/home/RedirectResultDemo
/// </summary>
/// <returns></returns>
public ActionResult RedirectResultDemo()
{
return Redirect(@"http://wwww.baidu.com");
} /// <summary>
/// RedirectToRouteResult的用法(跳转的action名称)
/// http://localhost:30735/home/RedirectToRouteResultDemo
/// </summary>
/// <returns></returns>
public ActionResult RedirectToRouteResultDemo()
{
return RedirectToAction(@"FileStreamResultDemo");
} /// <summary>
/// PartialViewResult的用法(返回部分视图)
/// http://localhost:30735/home/PartialViewResultDemo
/// </summary>
/// <returns></returns>
public PartialViewResult PartialViewResultDemo()
{
return PartialView();
} /// <summary>
/// ViewResult的用法(返回视图)
/// http://localhost:30735/home/ViewResultDemo
/// </summary>
/// <returns></returns>
public ActionResult ViewResultDemo()
{
//如果没有传入View名称, 默认寻找与Action名称相同的View页面.
return View();
}

ASP.NET MVC中常用的ActionResult类型的更多相关文章

  1. .Net Mvc学习——ASP.NET MVC中常用的ActionResult类型

    一.定义 MVC中ActionResult是Action的返回结果.ActionResult 有多个派生类,每个子类功能均不同,并不是所有的子类都需要返回视图View,有些直接返回流,有些返回字符串等 ...

  2. Asp.net MVC 中Controller返回值类型ActionResult

    [Asp.net MVC中Controller返回值类型] 在mvc中所有的controller类都必须使用"Controller"后缀来命名并且对Action也有一定的要求: 必 ...

  3. Asp .Net MVC中常用过滤属性类

    /// <summary> /// /// </summary> public class AjaxOnlyAttribute : ActionFilterAttribute ...

  4. ASP.NET MVC中Controller返回值类型ActionResult

    1.返回ViewResult视图结果,将视图呈现给网页 public class TestController : Controller { //必须存在Controller\Test\Index.c ...

  5. Asp.net mvc 中Action 方法的执行(二)

    [toc] 前面介绍了 Action 执行过程中的几个基本的组件,这里介绍 Action 方法的参数绑定. 数据来源 为 Action 方法提供参数绑定的原始数据来源于当前的 Http 请求,可能包含 ...

  6. MVC 中Controller返回值类型ActionResult

    下面列举Asp.net MVC中Controller中的ActionResult返回类型 1.返回ViewResult视图结果,将视图呈现给网页 public ActionResult About() ...

  7. dotNET开发之MVC中Controller返回值类型ActionResult方法总结

    1.返回ViewResult视图结果,将视图呈现给网页 2. 返回PartialViewResult部分视图结果,主要用于返回部分视图内容 3. 返回ContentResult用户定义的内容类型 4. ...

  8. Asp.Net MVC中DropDownListFor的用法(转)

    2016.03.04 扩展:如果 view中传入的是List<T>类型 怎么使用 DropList 既然是List<T> 那么我转化成 T  List<T>的第一个 ...

  9. Asp.net Mvc中利用ValidationAttribute实现xss过滤

    在网站开发中,需要注意的一个问题就是防范XSS攻击,Asp.net mvc中已经自动为我们提供了这个功能.用户提交数据时时,在生成Action参数的过程中asp.net会对用户提交的数据进行验证,一旦 ...

随机推荐

  1. codeforce150A(简单的求质数问题)

    A. Win or Freeze time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  2. 实力封装:Unity打包AssetBundle(三)

    前情提要:第二种打包方式 窗口初现 通过前面的两篇教程和一篇番外,我们已经完全实现了打包的功能.但是使用起来总是觉得有些晦涩别扭,如果不告诉别人怎么使用,对方肯定是一头雾水. What?你给我的这是啥 ...

  3. Linux 添加程序图标到开始菜单中

    Linux平台的Ubuntu系统中,开始菜单中的程序都在/usr/share/applications/目录下,文件格式都是xxxx.desktop ========================= ...

  4. shell脚本实例-跟踪网站日常变动

    #!/usr/bin/bash #用途:跟踪网页是否有更新 if [ $# -ne 1 ];then echo -e "$Usage $0 URl " exit fi first_ ...

  5. Python 类变量

    class Person: country = "大清" def __init__(self, name, gender): self.name = name self.gende ...

  6. HTTPS双向认证+USB硬件加密锁(加密狗)配置

    环境:  Ubuntu14.04,apache2.4.7, openssl1.0.1f 安装apache2 apt-get install apache2 -y 一般openssl默认已经安装 开启a ...

  7. 网络协议理论,http协议,数据结构,常用返回码

    一.网络协议理论 先是DNS协议 将域名转化成IP地址 这个你要知道 域名只是人记着方便 计算机记的是IP 然后是TCPIP协议 数据在传输过程中可能要经过陆游器 涉及到的是ARP协议 将IP地址转换 ...

  8. Visio 安装

    网上下载了光盘镜像文件,点击setup 安装,但是弹出来说少了文件,,msxml6,,,什么一大串,, 在网上下载了一个文件,,就可以用了,,,MicrosoftFixit50927.msi

  9. git clone新项目后如何拉取分支代码到本地

    1.git clone git@git.n.xxx.com:xxx/xxx.git 2.git fetch origin dev    命令来把远程dev分支拉到本地 3.checkout -b de ...

  10. 【c++基础】字符数组和string相互转换

    字符数组转化成string类型char ch [] = "ABCDEFG";string str(ch);//也可string str = ch;或者char ch [] = &q ...