码迷,mamicode.com
首页 > Web开发 > 详细

asp.net mvc5 step by step(四)——关于Controller的ActionResult

时间:2016-08-05 23:06:14      阅读:396      评论:0      收藏:0      [点我收藏+]

标签:

      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"); 
技术分享
技术分享}

三、Controller的Helper方法,可以返回对应的ActionResult
技术分享

 

asp.net mvc5 step by step(四)——关于Controller的ActionResult

标签:

原文地址:http://www.cnblogs.com/KevinMO/p/5742916.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!