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 ...
随机推荐
- php正则验证sql方注入
<?php function inject_check($Sql_Str) {//自动过滤Sql的注入语句. $check=preg_match('/select|insert|update|d ...
- laravel 心得
1.安装 使用composer安装laravel ,切换到你想要放置该网站的目录下,运行命令: composer create-project laravel/larevel project 4.1 ...
- 负载均衡集群之LVS持久链接
原理--> 通过构建一个hash表,利用CIP与RS的对应关系,来保持来自一个CIP的各种服务都走同一个RS 目的--> 保持持久链接的同时,将多个服务合并起来,例如http和https ...
- 学习第一个头文件stdio.h
使用标准输入输出库函数时要用到 “stdio.h”文件,因此源文件开头应有以下预编译命令: #include<stdio.h> stdio是standard input&outup ...
- linux的一点一滴---open
open函数用于打开和创建一个文件. 所需头文件: #include<sys/types.h> #include <sys/stat.h> #include <fcntl ...
- MySQL--query-cache
知识准备: 1.mysql 的query-cache是什么? mysql可以把执行完成的select 语句和这个select 语句对应的结果集缓存起来:下次再用调用相同的select 语句时就直接返 ...
- 在WPF中使用ArcGIS Engine
原文 http://blog.csdn.net/zzahkj/article/details/9102621 1.首先,新建一个WPF项目,添加引用ESRI.ArcGIS.AxControls.ESR ...
- Fatal error: Allowed memory size of 8388608 bytes exhausted
这两天安装bugfree,更换了一个数据量较大的库,结果打开bug详情页要么是空白页,要么就报如题的错误,错误信息还包括C:\wamp\www\bugfree\Include\Class\ADOLit ...
- 百度地图JavaScript API V1.5初级开发工具类
/** * 百度地图使用工具类-v1.5 * @author boonya * @date 2013-7-7 * @address Chengdu,Sichuan,China * @email boo ...
- poj 1077-Eight(八数码+逆向bfs打表)
The 15-puzzle has been around for over 100 years; even if you don't know it by that name, you've see ...