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. 问题汇总-20130927-关于rc.local命令无法执行

    场景:/etc/rc.local有语句 /usr/local/apache/bin/apachectl start mysql cactidb -u root -p123456 -e ' set gl ...

  2. Python学习(四) Python数据类型:序列(重要)

    插播一下,先了解一下Python的数据类型,Python现有的数据类型有好多,最重要的有列表.元组.字典 列表:我觉得可以对应java中的数组 list=['physics', 'chemistry' ...

  3. js阻止元素的默认事件与冒泡事件

    嵌套的div元素,如果父级和子元素都绑定了一些事件,那么在点击最内层子元素时可能会触发父级元素的事件,从而带来一定的影响. 1. event.preventDefault();  -- 阻止元素的默认 ...

  4. Google API v3 设置Icon问题处理

    1.查看API实现 //虽然比较符合API实现的思想但这个没法; //会产生Uncaught TypeError: undefined is not a function //google API n ...

  5. UESTC_Rain in ACStar 2015 UESTC Training for Data Structures<Problem L>

    L - Rain in ACStar Time Limit: 9000/3000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Other ...

  6. canvas.js | CLiPS

    canvas.js | CLiPS canvas.js The canvas.js module is a simple and robust JavaScript API for the HTML5 ...

  7. 机器学习 1、R语言

    R语言 R是用于统计分析.绘图的语言和操作环境.R是属于GNU系统的一个自由.免费.源代码开放的软件,它是一个用于统计计算和统计制图的优秀工具. 特点介绍 •主要用于统计分析.绘图.数据挖掘 •R内置 ...

  8. Unity 手指触摸的方向(单手)

    最近写了一个跑酷游戏,总结下里面的知识点:O(∩_∩)O~ using UnityEngine; using System.Collections; public class Demo : MonoB ...

  9. ios7 UITableView底线右移

    在ios7上UITableView底线右移了,我们可以通过添加代码来让它铺满整个屏幕的宽,在使用前要加上判断是否有这个方法 if ([_tableView respondsToSelector:@se ...

  10. Ice_cream's world I

    Ice_cream's world I Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) ...