Asp.net MVC 之 ActionResult
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的更多相关文章
- [转载]深入理解ASP.NET MVC之ActionResult
Action全局观 在上一篇最后,我们进行到了Action调用的“门口”: 1 if (!ActionInvoker.InvokeAction(ControllerContext, actionNam ...
- asp.net mvc之ActionResult
Web服务器接收到一个客户端请求以后,会对请求予以相应,而这个响应是通过Response来控制的, 但是在asp.net mvc 里,这部分的工作是由ActionResult来完成的, ActionR ...
- ASP.NET MVC自定义ActionResult实现文件压缩
有时候需要将单个或多个文件进行压缩打包后在进行下载,这里我自定义了一个ActionResult,方便进行文件下载 using System; using System.Collections; usi ...
- ASP.NET MVC 拓展ActionResult实现Html To Pdf 导出
之前实现了html直接转换为word文档的功能,那么是否也同样可以直接转换为pdf文档呢,网上搜了下html to pdf 的开源插件有很多 如:wkhtmltopdf,pdfsharp,itexts ...
- ASP.NET MVC 中 ActionResult 和 ViewResult 在使用上的区别
如果确认你返回的是一个视图(view),你可以直接返回类型为ViewResult. 如果你并不是很清楚,或者你根本不想去理解这些东西,你可以直接返回ActionResult
- Asp.net MVC 控制器ActionResult的例子
ActionResult 父类型 ViewResult View() 多重载应用 PartialViewResult PartialView() 部分试图 New EmptyResult() 空 如 ...
- Asp.net MVC 之ActionResult
ActionResult 派生出以下子类: ViewResult 返回一个网页视图 PartialViewResult 返回一个网页视图,但不适用布局页. ContentResult 返回一段字符串文 ...
- ASP.NET MVC中ActionResult的不同返回方式
1.返回视图 return View();//返回方法名对应的视图 return View("aaa");//返回名称为aaa的视图 2.返回文本内容 return Content ...
- 【转】ASP.NET MVC学习笔记-Controller的ActionResult
1. 返回ViewResult public ActionResult Index() { ViewData["Message"] = "Welcome ...
随机推荐
- 如何让EcStore和微博同步来推广网站
EcStore是创建B2C商城的首选PHP系统,它功能强大.操作方便,安装后马上就能建立起一个自己的B2C商城,但建好后如何推广运营商城却不是件容易的事. 新浪微博用户数量大.传播速度快,互联网上拥有 ...
- SQL触发器学习
简介 触发器是一种特殊类型的存储过程.触发器分为: DML( 数据操纵语言 Data Manipulation Language)触发器:数据库中表或视图的数据更改时触发,包括insert,upd ...
- linq分组查询
string[] arrStr = { ".com", "www.baidu.com", "www.qq.com", "www.b ...
- Java所有编码问题参考手册
一.编码基本知识 1.iso8859-1 ——属于单字节编码,最多能表示的字符范围是 0-255,应用于英文系列.比如,字母 'a' 的编码为0x61=97. 很明显,iso8859-1 编码表示的 ...
- cf479D Long Jumps
D. Long Jumps time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- printf "%.*s" 分类: 小细节 2015-07-04 14:36 2人阅读 评论(0) 收藏
ref : http://www.cnblogs.com/yuaqua/archive/2011/10/21/2219856.html 小数点.后"*"表示输出位数,具体的数据来自 ...
- The Java™ Tutorials下载地址
1.The Java™ Tutorials下载地址: http://www.oracle.com/technetwork/java/javase/java-tutorial-downloads-200 ...
- cocos2dx lua 学习笔记(二)
安装开发环境 sublime - http://www.sublimetext.com/2 package control - http://packagecontrol.io/installatio ...
- Android launcher3 开发初始篇
版本号:1.0 日期:2014.8.26 2014.8.27 2014.11.10 版权:© 2014 kince 转载注明出处 好久没有写博客,也是由于工作比較忙的关系.当然这不是理 ...
- Lua多重继承
http://blog.csdn.net/ssihc0/article/details/7742421 代码收藏了,以后用的到 --多重继承 local function search(k,plist ...