ActionResult是控制器方法执行后返回的结果类型,控制器方法可以返回一个直接或间接从ActionResult抽象类继承的类型,如果返回的是非ActionResult类型,控制器将会将结果转换为一个ContentResult类型。默认的ControllerActionInvoker调用ActionResult.ExecuteResult方法生成应答结果。

一、ActionResult派生类关系图

二、常见的几种ActionResult

1、ContentResult

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

 
C# 代码   复制

public ContentResult RSSFeed()

{

Story[] stories = GetAllStories(); // Fetch them from the database or wherever



// Build the RSS feed document

string encoding = Response.ContentEncoding.WebName;

XDocument rss = new XDocument(new XDeclaration("1.0", encoding, "yes"),

new XElement("rss", new XAttribute("version", "2.0"),

new XElement("channel", new XElement("title", "Example RSS 2.0 feed"),

from story in stories

select new XElement("item",

new XElement("title", story.Title),

new XElement("description", story.Description),

new XElement("link", story.Url)

)

)

)

);

return Content(rss.ToString(), "application/rss+xml");

}

2、EmptyResult

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

3、RedirectResult

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

 
C# 代码   复制

public override void ExecuteResult(ControllerContext context) {

if (context == null) {

throw new ArgumentNullException("context");

}

if (context.IsChildAction) {

throw new InvalidOperationException(MvcResources.RedirectAction_CannotRedirectInChildAction);

}



string destinationUrl = UrlHelper.GenerateContentUrl(Url, context.HttpContext);

context.Controller.TempData.Keep();

context.HttpContext.Response.Redirect(destinationUrl, false /* endResponse */);

}

4、RedirectToRouteResult

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

5、ViewResult:

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

6、PartialViewResult:

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

7、HttpUnauthorizedResult:

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

8、JavaScriptResult:

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

9、JsonResult:

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

 
C# 代码   复制

class CityData { public string city; public int temperature; }

public JsonResult WeatherData()

{

var citiesArray = new[] {

new CityData { city = "London", temperature = 68 },

new CityData { city = "Hong Kong", temperature = 84 }

};

return Json(citiesArray);

}

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

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

 
C# 代码   复制

public FilePathResult DownloadReport()

{

string filename = @"c:\\files\\somefile。pdf";

return File(filename, "application/pdf", "AnnualReport。pdf");

}

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

 
C# 代码   复制



public FileContentResult GetImage(int productId)

{

var product = productsRepository.Products.First(x => x.ProductID == productId);

return File(product.ImageData, product.ImageMimeType);

}

<img src="<%: Url.Action("GetImage", "Products", new { Model.ProductID }) %>" />

FileStreamResult:返回流

 
C# 代码   复制

public FileStreamResult ProxyExampleDotCom()

{

WebClient wc = new WebClient();

Stream stream = wc.OpenRead(http://www.studyofnet.com/);

return File(stream, "text/html");

}

MVC中的ActionResult的更多相关文章

  1. .NET MVC中的ActionResult

    一  摘要 本文介绍了ASP.NET MVC中的ActionResult,本节主要介绍 EmptyResult / Content Result /JavaScriptResult /JsonResu ...

  2. 理解ASP.NET MVC中的ActionResult

    通常我们在一个ASP.NET MVC项目中创建一个Controller的时候,Index()方法默认的返回类型都是ActionResult,通过查看UML图,ActionResult实际上是一个抽象类 ...

  3. asp.net core mvc中自定义ActionResult

    在GitHub上有个项目,本来是作为自己研究学习.net core的Demo,没想到很多同学在看,还给了很多星,所以觉得应该升成3.0,整理一下,写成博分享给学习.net core的同学们. 项目名称 ...

  4. ASP.NET MVC中多种ActionResult用法总结

    最近一段时间做了个ASP.NET MVC4.0的项目,项目马上就要结束了,今天忙里偷闲简单总结一下心得: 1. 如果Action需要有返回值的话,必须是ActionResult的话,可以返回一个Emp ...

  5. MVC中几种常用ActionResult

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

  6. ASP.NET MVC中常用的ActionResult类型

    常见的ActionResult 1.ViewResult 表示一个视图结果,它根据视图模板产生应答内容.对应得Controller方法为View. 2.PartialViewResult 表示一个部分 ...

  7. [转]MVC中几种常用ActionResult

    本文转自:http://www.cnblogs.com/xielong/p/5940535.html 一.定义 MVC中ActionResult是Action的返回结果.ActionResult 有多 ...

  8. MVC中几种常用的ActionResult

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

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

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

随机推荐

  1. lintcode : 空格替换

    题目: 空格替换 设计一种方法,将一个字符串中的所有空格替换成 %20 .你可以假设该字符串有足够的空间来加入新的字符,且你得到的是“真实的”字符长度. 样例 对于字符串"Mr John S ...

  2. ASP.NET MVC 3 初认知

    什么是ASP.NET MVC 1. asp.net mvc 是微软官方提供的mvc模式编写asp.net web应用程序的框架. 2. 是微软既asp.net webForm 后的又一种开放方式,而非 ...

  3. cv 论文(CNN相关)

    最近发现很多以前看的论文都忘了,所以想写点东西来整理下之前的paper,paper主要是cv(computer vision)方向的. 第一篇:Gradient-based learning appl ...

  4. Oracle配置详解

    [Oracle连接字符串][Oracle Net Manager 服务命名配置][PL/SQL 登陆数据库] 连接数据库的几个重要参数: 1. 登陆用户名:user: 2. 登录密码:password ...

  5. AngularJS初探:搭建PhoneCat项目的开发与测试环境

    AngularJS官方网站提供了一个用于学习的示例项目:PhoneCat.这是一个Web应用,用户可以浏览一些Android手机,了解它们的详细信息,并进行搜索和排序操作. 对于PhoneCat项目的 ...

  6. JUnit单元测试--小试牛刀

    单元测试更多的是在开发阶段完成,开发人员每写一个函数的时候都会写相应的单元测试.对于java代码,普遍使用的是jUnit,根据jUnit可以自己相应的开发一套自动化测试框架.这个的前提是要学会juni ...

  7. IOS设置背景色设置最简单方法

    [self.view setBackgroundColor:[UIColor clearColor]];

  8. NDK(15)在ndk代码中注册和注销native函数

    转自:  http://www.cnblogs.com/canphp/archive/2012/11/13/2768937.html C和C++注册native函数的方式大致上相同,下面给出具体的代码 ...

  9. oracle portlist.ini

    Enterprise Manager Database Control URL - (orcl) :https://redhat4.7:1158/em [root@redhat4 install]# ...

  10. ie下jquery ajax 80020101错误的解决方法

    <script language="javascript">    <!--    function checkAll(name,isCheck){       ...