MVC的12种ActionResult介绍以及应用示例【转】
一、介绍
1、ViewResult
表示一个视图结果,它根据视图模板产生应答内容。对应得Controller方法为View。
2、PartialViewResult
表示一个部分视图结果,与ViewResult本质上一致,只是部分视图不支持母版,对应于ASP.NET,ViewResult相当于一个Page,而PartialViewResult 则相当于一个UserControl。它对应得Controller方法的PartialView.
3、RedirectResult
表示一个连接跳转,相当于ASP.NET中的Response.Redirect方法,对应得Controller方法为Redirect。
4、RedirectToRouteResult
同样表示一个跳转,MVC会根据我们指定的路由名称或路由信息(RouteValueDictionary)来生成Url地址,然后调用Response.Redirect跳转。对应的Controller方法为RedirectToAction和RedirectToRoute.
5、ContentResult
返回简单的纯文本内容,可通过ContentType属性指定应答文档类型,通过ContentEncoding属性指定应答文档的字符编码。可通过Controller类中的Content方法便捷地返回ContentResult对象。如果控制器方法返回非ActionResult对象,MVc将简单地以返回对象的toString()内容为基础产生一个ContentResult对象。
6、EmptyResult
返回一个空的结果,如果控制器方法返回一个null ,MVC将其转换成EmptyResult对象。
7、JavaScriptResult
本质上是一个文本内容,只是将Response.ContentType设置为application/x-javascript,此结果应该和MicrosoftMvcAjax.js脚本配合使用,客户端接收到Ajax应答后,将判断Response.ContentType的值,如果是application/x-javascript,则直接eval 执行返回的应答内容,此结果类型对应得Controller方法为JavaScript.
8、JsonResult
表示一个Json结果。MVC将Response.ContentType 设置为application/json,并通过JavaScriptSerializer类指定对象序列化为Json表示方式。需要注意,默认情况下,Mvc不允许GET请求返回Json结果,要解除此限制,在生成JsonResult对象时,将其JsonRequestBehavior属性设置为JsonRequestBehavior.AllowGet,此结果对应Controller方法的Json.
9、FileResult(FilePathResult、FileContentResult、FileStreamResult)
这三个类继承于FileResult,表示一个文件内容,三者区别在于,FilePath 通过路径传送文件到客户端,FileContent 通过二进制数据的方式,而FileStream 是通过Stream(流)的方式来传送。Controller为这三个文件结果类型提供了一个名为File的重载方法。
FilePathResult: 直接将一个文件发送给客户端
FileContentResult: 返回byte字节给客户端(比如图片)
FileStreamResult: 返回流
10、HttpUnauthorizedResult
表示一个未经授权访问的错误,MVC会向客户端发送一个401的应答状态。如果在web.config 中开启了表单验证(authenication mode=”Forms”),则401状态会将Url 转向指定的loginUrl 链接。
11、HttpStatusCodeResult
返回一个服务器的错误信息
12、HttpNoFoundResult
返回一个找不到Action错误信息
二、ActionResult子类之间的关系表

三、应用示例
using StudyMVC4.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Mvc; namespace StudyMVC4.Controllers
{
public class HomeController : Controller
{ public ActionResult Index() {
return View();
} /// <summary>
/// ContentResult用法(返回文本)
/// http://localhost:30735/home/ContentResultDemo
/// </summary>
/// <returns>返回文本</returns>
public ActionResult ContentResultDemo(){
string str = "ContentResultDemo!";
return Content(str);
} /// <summary>
/// EmptyResult的用法(返回空对象)
/// http://localhost:30735/home/EmptyResultDemo
/// </summary>
/// <returns>返回一个空对象</returns>
public ActionResult EmptyResultDemo (){
return new EmptyResult();
} /// <summary>
/// FileContentResult的用法(返回图片)
/// http://localhost:30735/home/FileContentResultDemo
/// </summary>
/// <returns>显示一个文件内容</returns>
public ActionResult FileContentResultDemo() {
FileStream fs = new FileStream(Server.MapPath(@"/Images/001.jpg"), FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[Convert.ToInt32(fs.Length)];
fs.Read(buffer, 0, Convert.ToInt32(fs.Length));
string contentType = "image/jpeg";
return File(buffer, contentType);
} /// <summary>
/// FilePathResult的用法(返回图片)
/// http://localhost:30735/home/FilePathResultDemo/002
/// </summary>
/// <param name="id">图片id</param>
/// <returns>直接将返回一个文件对象</returns>
public FilePathResult FilePathResultDemo(string id)
{
string path = Server.MapPath(@"/Images/"+id +".jpg");
//定义内容类型(图片)
string contentType = "image/jpeg";
//FilePathResult直接返回file对象
return File(path, contentType);
} /// <summary>
/// FileStreamResult的用法(返回图片)
/// http://localhost:30735/home/FileStreamResultDemo
/// </summary>
/// <returns>返回文件流(图片)</returns>
public ActionResult FileStreamResultDemo()
{
FileStream fs = new FileStream(Server.MapPath(@"/Images/001.jpg"), FileMode.Open, FileAccess.Read);
string contentType = "image/jpeg";
return File(fs, contentType);
} /// <summary>
/// HttpUnauthorizedResult 的用法(抛出401错误)
/// http://localhost:30735/home/HttpUnauthorizedResult
/// </summary>
/// <returns></returns>
public ActionResult HttpUnauthorizedResultDemo()
{
return new HttpUnauthorizedResult();
} /// <summary>
/// HttpStatusCodeResult的方法(返回错误状态信息)
/// http://localhost:30735/home/HttpStatusCodeResult
/// </summary>
/// <returns></returns>
public ActionResult HttpStatusCodeResultDemo() {
return new HttpStatusCodeResult(500, "System Error");
} /// <summary>
/// HttpNotFoundResult的使用方法
/// http://localhost:30735/home/HttpNotFoundResultDemo
/// </summary>
/// <returns></returns>
public ActionResult HttpNotFoundResultDemo() {
return new HttpNotFoundResult("not found action");
} /// <summary>
/// JavaScriptResult 的用法(返回脚本文件)
/// http://localhost:30735/home/JavaScriptResultDemo
/// </summary>
/// <returns>返回脚本内容</returns>
public ActionResult JavaScriptResultDemo()
{
return JavaScript(@"<script>alert('Test JavaScriptResultDemo!')</script>");
} /// <summary>
/// JsonResult的用法(返回一个json对象)
/// http://localhost:30735/home/JsonResultDemo
/// </summary>
/// <returns>返回一个json对象</returns>
public ActionResult JsonResultDemo()
{
var tempObj = new { Controller = "HomeController", Action = "JsonResultDemo" };
return Json(tempObj);
} /// <summary>
/// RedirectResult的用法(跳转url地址)
/// http://localhost:30735/home/RedirectResultDemo
/// </summary>
/// <returns></returns>
public ActionResult RedirectResultDemo()
{
return Redirect(@"http://wwww.baidu.com");
} /// <summary>
/// RedirectToRouteResult的用法(跳转的action名称)
/// http://localhost:30735/home/RedirectToRouteResultDemo
/// </summary>
/// <returns></returns>
public ActionResult RedirectToRouteResultDemo()
{
return RedirectToAction(@"FileStreamResultDemo");
} /// <summary>
/// PartialViewResult的用法(返回部分视图)
/// http://localhost:30735/home/PartialViewResultDemo
/// </summary>
/// <returns></returns>
public PartialViewResult PartialViewResultDemo()
{
return PartialView();
} /// <summary>
/// ViewResult的用法(返回视图)
/// http://localhost:30735/home/ViewResultDemo
/// </summary>
/// <returns></returns>
public ActionResult ViewResultDemo()
{
//如果没有传入View名称, 默认寻找与Action名称相同的View页面.
return View();
}
}
}
本文转自:https://www.cnblogs.com/xielong/p/5940535.html
MVC的12种ActionResult介绍以及应用示例【转】的更多相关文章
- MVC中几种常用ActionResult
一.定义 MVC中ActionResult是Action的返回结果.ActionResult 有多个派生类,每个子类功能均不同,并不是所有的子类都需要返回视图View,有些直接返回流,有些返回字符串等 ...
- 了解ASP.NET MVC几种ActionResult的本质:JavaScriptResult & JsonResult
在之前的两篇文章(<EmptyResult & ContentResult>和<FileResult>)我们剖析了EmptyResult.ContentResult和F ...
- [转]MVC中几种常用ActionResult
本文转自:http://www.cnblogs.com/xielong/p/5940535.html 一.定义 MVC中ActionResult是Action的返回结果.ActionResult 有多 ...
- MVC中几种常用的ActionResult
一.定义 MVC中ActionResult是Action的返回结果.ActionResult 有多个派生类,每个子类功能均不同,并不是所有的子类都需要返回视图View,有些直接返回流,有些返回字符串等 ...
- 12种JavaScript MVC框架之比较
Gordon L. Hempton是西雅图的一位黑客和设计师,他花费了几个月的时间研究和比较了12种流行的JavaScript MVC框架,并在博客中总结了每种框架的优缺点,最终的结果是,Ember. ...
- 转:12种JavaScript MVC框架之比较
Gordon L. Hempton是西雅图的一位黑客和设计师,他花费了几个月的时间研究和比较了12种流行的JavaScript MVC框架,并在博客中总结了每种框架的优缺点,最终的结果是,Ember. ...
- javascript:12种JavaScript MVC框架之比较
Gordon L. Hempton是西雅图的一位黑客和设计师,他花费了几个月的时间研究和比较了12种流行的JavaScript MVC框架,并在博客中总结了每种框架的优缺点,最终的结果是,Ember. ...
- [转]12种JavaScript MVC框架之比较
From : http://www.infoq.com/cn/news/2012/05/js-mvc-framework/ Gordon L. Hempton是西雅图的一位黑客和设计师,他花费了几个月 ...
- 认识ASP.NET MVC的5种AuthorizationFilter
在总体介绍了筛选器及其提供机制(<深入探讨ASP.NET MVC的筛选器>)之后,我们按照执行的先后顺序对四种不同的筛选器进行单独介绍,首先来介绍最先执行的AuthorizationFil ...
随机推荐
- uefi是如何启动linux内核的?
答:uefi启动linux内核有两条路径: 1. uefi直接进入uefi shell来启动linux内核 2. uefi直接进入uefi shell启动grub启动器,然后进入grub shell启 ...
- A filter or servlet of the current chain does not support asynchronous operations. 错误解决记录
做视频文件上传一直报这个错误: java.lang.IllegalStateException: A filter or servlet of the current chain does not s ...
- C语言中的异常处理
一 前言: 异常处理,对于做面向对象开发的开发者来说是再熟悉不过了,例如在C#中有 try { ... } catch( Exception e){...} finally{ ..... } 在C++ ...
- 26 Flutter仿京东商城项目 购物车之 event_bus事件广播 事件监听
event_bus 介绍 在前面的课程我们给大家讲过状态管理 Provider 的使用. 通俗的讲状态管理就是:当我们想在多个页面(组件/Widget)之间共享状态(数据),或 者一个页面(组件/Wi ...
- spring security4.1.3配置以及踩过的坑
https://blog.csdn.net/honghailiang888/article/details/53520557 spring security完全可以作为一个专门的专题来说,有一个专题写 ...
- Masonry详解
- (void)viewDidLoad { [super viewDidLoad]; //1.view1 居中显示 UIView *view1 = [[UIView alloc]init]; view ...
- win7下安装IIS7
在Windows 7下如何安装IIS7,以及IIS7在安装过程中的一些需要注意的设置,以及在IIS7下配置ASP的正确方法. 在Windows 7下面IIS7的安装方法: 一.进入Windows 7的 ...
- 【ARM-Linux开发】【QT开发】Couldnt load module input, no modules loaded
export TSLIB_FBDEVICE=/dev/fb0 export TSLIB_PLUGINDIR=/usr/lib/ts (input.so 所在目录)
- K8S 从入门到放弃系列文章目录(Kubernetes 1.14)
1)软件环境 软件 版本 系统 Centos7.5 Kubernetes 1.14.1 Docker 18.09 Calico 3.6 Etcd 3.3.12 2)部署过程简单概要 三台master节 ...
- java读写cookie中文乱码解决方法
1.写入的时候: public boolean addCookie( HttpServletRequest req, HttpServletResponse resp){ //创建 Cookie co ...