Action运行完后,回传的值通过ActionResult 类别或者其衍生的类别操作。ActionResult是一个抽象类,因此,Asp.net MVC 本身就实作了许多不同类型的ActionResult的子类别。

ActionResult 子类以及说明:

常用的ViewResult用来回传一个View,即HTML的页面内容;

PartialViewResult用来回传一个View,但是这个View是PartialView;

RedirectResult用来将网页转向其他的网址;

EmptyResult用来返回一个空白的页面;

ContentResult返回一个文字属性(文本内容);

FileResult返回一个二进制的文档;

FileContentResult 返回一个可以下载的二进制文件;

FilePathResult返回一个可以下载的并且制定路径的二进制文件;

FileStreamResult返回一个可以下载的流式文件;

JsonResult返回一个JSON结果;

JavaScriptResult返回一个JavaScript对象。

这些都是继承自ActionResult的类别,也可以用来当做Action 的类型。

但是我们经常在Controller中定义的返回类型为ActionResult, 但是返回的值经常是别的,比如:

 //
// GET: /Product/
public ActionResult Index()
{
return View();
}

这是以为View返回的对象是ViewResult.

Controller常见的方法Redirect返回的对象是RedirectResult;

RedirectToAction返回的对象是RedirectToActionResult;

RedirectToRoute返回的对象是RedirectToRouteResult;

Json返回的对象是JsonResult;

JavaScriptResult返回的对象是JavaScriptResult;

Content返回的对象是ContentResult;

File返回的对象是FileContentResult、FilePathResult、FileStreamResult等;

下面是几个例子

返回PartialView

        public ActionResult ProductList()
{
ProductBLL productBLL = new ProductBLL(HttpContext.Application["EFConnectionString"].ToString()); IEnumerable<Product> products = productBLL.ListProducts(); return PartialView("~/Views/InStock/_Products.cshtml",products);
}

返回Json

         public ActionResult RetrieveProduct(int id)
{
ProductBLL productBLL = new ProductBLL(HttpContext.Application["EFConnectionString"].ToString()); Product product = productBLL.RetrieveProduct(id); return Json(product,JsonRequestBehavior.AllowGet);
}

返回pdf

         public ActionResult LoadPdfFile()
{
string path = @"C:\ZJF\My Team Solutions\Allure\Web\BackOfficeV2\Google_Merchant_Center_快速操作手册.pdf";
FileStream stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite); return File(stream, "application/pdf");
}

Asp.net MVC 之 ActionResult的更多相关文章

  1. [转载]深入理解ASP.NET MVC之ActionResult

    Action全局观 在上一篇最后,我们进行到了Action调用的“门口”: 1 if (!ActionInvoker.InvokeAction(ControllerContext, actionNam ...

  2. asp.net mvc之ActionResult

    Web服务器接收到一个客户端请求以后,会对请求予以相应,而这个响应是通过Response来控制的, 但是在asp.net mvc 里,这部分的工作是由ActionResult来完成的, ActionR ...

  3. ASP.NET MVC自定义ActionResult实现文件压缩

    有时候需要将单个或多个文件进行压缩打包后在进行下载,这里我自定义了一个ActionResult,方便进行文件下载 using System; using System.Collections; usi ...

  4. ASP.NET MVC 拓展ActionResult实现Html To Pdf 导出

    之前实现了html直接转换为word文档的功能,那么是否也同样可以直接转换为pdf文档呢,网上搜了下html to pdf 的开源插件有很多 如:wkhtmltopdf,pdfsharp,itexts ...

  5. ASP.NET MVC 中 ActionResult 和 ViewResult 在使用上的区别

    如果确认你返回的是一个视图(view),你可以直接返回类型为ViewResult. 如果你并不是很清楚,或者你根本不想去理解这些东西,你可以直接返回ActionResult

  6. Asp.net MVC 控制器ActionResult的例子

    ActionResult 父类型 ViewResult View() 多重载应用 PartialViewResult PartialView() 部分试图 New EmptyResult()  空 如 ...

  7. Asp.net MVC 之ActionResult

    ActionResult 派生出以下子类: ViewResult 返回一个网页视图 PartialViewResult 返回一个网页视图,但不适用布局页. ContentResult 返回一段字符串文 ...

  8. ASP.NET MVC中ActionResult的不同返回方式

    1.返回视图 return View();//返回方法名对应的视图 return View("aaa");//返回名称为aaa的视图 2.返回文本内容 return Content ...

  9. 【转】ASP.NET MVC学习笔记-Controller的ActionResult

    1. 返回ViewResult public ActionResult Index()   {       ViewData["Message"] = "Welcome ...

随机推荐

  1. arcpy批量打印地图

    有个处理数据的需求是把一个图层中的要素单独显示在底图上,设置固定的比例尺,并打印出图片. 考虑到后续会有重复的大量的数据要处理,决定使用arcpy处理. 首先新建一个mxd底图文档,把需要打印的地图都 ...

  2. LeetCode_Jump Game II

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  3. Keil C51 与 ARM 并存的方法

    很多朋友都在想,怎么让keil C51与ARM能够并存使用.有安装经验的朋友都知道,安好C51后再安ARm,C51不能正常工作:安好ARM后再安C51,ARM不能正常工作. 网上也有相关解决办法,不过 ...

  4. HtmlNodeType枚举

    HtmlNodeType是一个枚举,用于说明一个节点的类型. 源代码如下所示: public enum HtmlNodeType { Document = 0, Element = 1, Commen ...

  5. SmartBusinessDevFramework架构设计-3:考虑开源?

    掖着藏着,终归不是好的办法.说的跟花一样,究竟里子是什么东西.一个好的被子,里料是羽绒还是棉花还是丝绵还是黑心棉?有时候,真的是看过之后,才能体验其中的奥秘. 这个架构的设计初衷,总体是为了方便.ne ...

  6. Google map v3 using simple tool file google.map.util.js v 1.0

    /** * GOOGLE地图开发使用工具 * @author BOONYACHENGDU@GMAIL.COM * @date 2013-08-23 * @notice 地图容器的(div)z-inde ...

  7. js中的注意事项(持续整理)

    1.兼容性 <div class="toutiao_r fl_r" id ="toutiao_r"></div> 这个div中有两个样式 ...

  8. Unity 音乐播放全局类

    今天晚了LOL, 发现里面的声音系统做得很不错,于是最近就写了一份反正以后也用的到,2D音乐全局播放. 项目跟PoolManager对象池插件结合了. 解决的问题: 1. 已经播放的声音,可以马上暂停 ...

  9. 如何把UIView转成UIImage,解决模糊失真问题

    最近工作中,遇到一个需求,需要把一个UIView对象转成UIImage对象显示.经过网络搜索,找到如下答案: ? 1 2 3 4 5 6 7 8 -(UIImage*)convertViewToIma ...

  10. dom4j解析XML的CURD操作

    在开发JavaWeb项目中通常是利用XML配置文件来提高应用程序的访问效率,而在配置XML的同时,更多时候是对XML进行解析. 一.解析XML配置文件有两种机制: DOM和SAX的机制: DOM是将整 ...