一、介绍

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介绍以及应用示例【转】的更多相关文章

  1. MVC中几种常用ActionResult

    一.定义 MVC中ActionResult是Action的返回结果.ActionResult 有多个派生类,每个子类功能均不同,并不是所有的子类都需要返回视图View,有些直接返回流,有些返回字符串等 ...

  2. 了解ASP.NET MVC几种ActionResult的本质:JavaScriptResult & JsonResult

    在之前的两篇文章(<EmptyResult & ContentResult>和<FileResult>)我们剖析了EmptyResult.ContentResult和F ...

  3. [转]MVC中几种常用ActionResult

    本文转自:http://www.cnblogs.com/xielong/p/5940535.html 一.定义 MVC中ActionResult是Action的返回结果.ActionResult 有多 ...

  4. MVC中几种常用的ActionResult

    一.定义 MVC中ActionResult是Action的返回结果.ActionResult 有多个派生类,每个子类功能均不同,并不是所有的子类都需要返回视图View,有些直接返回流,有些返回字符串等 ...

  5. 12种JavaScript MVC框架之比较

    Gordon L. Hempton是西雅图的一位黑客和设计师,他花费了几个月的时间研究和比较了12种流行的JavaScript MVC框架,并在博客中总结了每种框架的优缺点,最终的结果是,Ember. ...

  6. 转:12种JavaScript MVC框架之比较

    Gordon L. Hempton是西雅图的一位黑客和设计师,他花费了几个月的时间研究和比较了12种流行的JavaScript MVC框架,并在博客中总结了每种框架的优缺点,最终的结果是,Ember. ...

  7. javascript:12种JavaScript MVC框架之比较

    Gordon L. Hempton是西雅图的一位黑客和设计师,他花费了几个月的时间研究和比较了12种流行的JavaScript MVC框架,并在博客中总结了每种框架的优缺点,最终的结果是,Ember. ...

  8. [转]12种JavaScript MVC框架之比较

    From : http://www.infoq.com/cn/news/2012/05/js-mvc-framework/ Gordon L. Hempton是西雅图的一位黑客和设计师,他花费了几个月 ...

  9. 认识ASP.NET MVC的5种AuthorizationFilter

    在总体介绍了筛选器及其提供机制(<深入探讨ASP.NET MVC的筛选器>)之后,我们按照执行的先后顺序对四种不同的筛选器进行单独介绍,首先来介绍最先执行的AuthorizationFil ...

随机推荐

  1. 使用PHP实现查找附近的人

    https://zhuanlan.zhihu.com/p/31380780 LBS(基于位置的服务) 查找附近的人有个更大的专有名词叫做LBS(基于位置的服务),LBS是指是指通过电信移动运营商的无线 ...

  2. OS X环境下如何搭建编译Cocos2D-X v3.x的Android Studio工程

    Cocos2D-X官网已经简单介绍了如何在OS X环境下搭建Cocos2D-X v2.x和v3.x的指南.具体链接为:http://www.cocos.com/doc/article/index?ty ...

  3. 初始化jsp页面下拉选备选项【我】

    将下列 script标签放到页面的最下端 <!-- 初始化的全局变量,供js中使用,主要拼接下拉选的初始化值 --> <script type="text/javascri ...

  4. 阶段5 3.微服务项目【学成在线】_day16 Spring Security Oauth2_01-用户认证需求分析

    1.1 用户认证与授权 截至目前,项目已经完成了在线学习功能,用户通过在线学习页面点播视频进行学习.如何去记录学生的学习过程 呢?要想掌握学生的学习情况就需要知道用户的身份信息,记录哪个用户在什么时间 ...

  5. Ehcache开启JMX支持

    Ehcache提供了基于JMX的监控支持,支持对以下几类信息的监控. CacheManager Cache CacheConfiguration CacheStatistics 按照JMX的规范,为了 ...

  6. iOS-UIScrollView滚动视图(转)

    http://blog.csdn.net/iukey/article/details/7319314 UIScrollView 类负责所有基于 UIKit 的滚动操作. 一.创建 CGRect bou ...

  7. Docker 容器的通信(十二)

    目录 一.容器间通信 1.IP 通信 2.Docker DNS Server 3.joined 容器 二.容器访问外部网络 三.外部网络访问容器 1.随机端口 2.指定端口 3.不指定任何端口. 4. ...

  8. 从源码角度解析Netty的React模式是如何工作的

    Netty 支持多种实现方式,比如nio,epoll 等,本文以nio的实现方式进行讲解. 1.EventLoop : 事件循环看,简单来说就是一个死循环监听事件,如果事件来了,处理掉.通常做法就是开 ...

  9. SpringCloud入门01之基础知识

    一.Spring Cloud 什么是spring cloud, 为什么要使用微服务架构? 参考度娘 Spring Cloud是一系列框架的有序集合, 它利用Spring Boot的开发便利性巧妙地简化 ...

  10. Struts2中action接收中文参数为乱码解决方法

    老实说,中文乱码问题是每个程序员会经常遇到的问题,而且也是一个很头疼的问题.网上很多关于解决中文乱码的帖子,看几个之后你会发现大都是一样的.但是我们照着做,却还是无法解决乱码问题.我也是看了好多帖子, ...