一、定义

MVC中ActionResult是Action的返回结果。ActionResult 有多个派生类,每个子类功能均不同,并不是所有的子类都需要返回视图View,有些直接返回流,有些返回字符串等。ActionResult是一个抽象类,它定义了唯一的ExecuteResult方法,参数为一个ControllerContext,下面为您介绍MVC中的ActionResult 的用法。

二、什么是ActionResult

ActionResult是控制器方法执行后返回的结果类型,控制器方法可以返回一个直接或间接从ActionResult抽象类继承的类型,如果返回的 是非ActionResult类型,控制器将会将结果转换为一个ContentResult类型。默认的ControllerActionInvoker 调用ActionResult.ExecuteResult方法生成应答结果。

三、常见的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子类之间的关系表

     

五、ActionResult(12种)的简单应用

源码:

  1 using StudyMVC4.Models;
2 using System;
3 using System.Collections.Generic;
4 using System.IO;
5 using System.Linq;
6 using System.Net;
7 using System.Net.Http;
8 using System.Web.Http;
9 using System.Web.Mvc;
10
11 namespace StudyMVC4.Controllers
12 {
13 public class HomeController : Controller
14 {
15
16 public ActionResult Index() {
17 return View();
18 }
19
20 /// <summary>
21 /// ContentResult用法(返回文本)
22 /// http://localhost:30735/home/ContentResultDemo
23 /// </summary>
24 /// <returns>返回文本</returns>
25 public ActionResult ContentResultDemo(){
26 string str = "ContentResultDemo!";
27 return Content(str);
28 }
29
30 /// <summary>
31 /// EmptyResult的用法(返回空对象)
32 /// http://localhost:30735/home/EmptyResultDemo
33 /// </summary>
34 /// <returns>返回一个空对象</returns>
35 public ActionResult EmptyResultDemo (){
36 return new EmptyResult();
37 }
38
39 /// <summary>
40 /// FileContentResult的用法(返回图片)
41 /// http://localhost:30735/home/FileContentResultDemo
42 /// </summary>
43 /// <returns>显示一个文件内容</returns>
44 public ActionResult FileContentResultDemo() {
45 FileStream fs = new FileStream(Server.MapPath(@"/Images/001.jpg"), FileMode.Open, FileAccess.Read);
46 byte[] buffer = new byte[Convert.ToInt32(fs.Length)];
47 fs.Read(buffer, 0, Convert.ToInt32(fs.Length));
48 string contentType = "image/jpeg";
49 return File(buffer, contentType);
50 }
51
52 /// <summary>
53 /// FilePathResult的用法(返回图片)
54 /// http://localhost:30735/home/FilePathResultDemo/002
55 /// </summary>
56 /// <param name="id">图片id</param>
57 /// <returns>直接将返回一个文件对象</returns>
58 public FilePathResult FilePathResultDemo(string id)
59 {
60 string path = Server.MapPath(@"/Images/"+id +".jpg");
61 //定义内容类型(图片)
62 string contentType = "image/jpeg";
63 //FilePathResult直接返回file对象
64 return File(path, contentType);
65 }
66
67 /// <summary>
68 /// FileStreamResult的用法(返回图片)
69 /// http://localhost:30735/home/FileStreamResultDemo
70 /// </summary>
71 /// <returns>返回文件流(图片)</returns>
72 public ActionResult FileStreamResultDemo()
73 {
74 FileStream fs = new FileStream(Server.MapPath(@"/Images/001.jpg"), FileMode.Open, FileAccess.Read);
75 string contentType = "image/jpeg";
76 return File(fs, contentType);
77 }
78
79 /// <summary>
80 /// HttpUnauthorizedResult 的用法(抛出401错误)
81 /// http://localhost:30735/home/HttpUnauthorizedResult
82 /// </summary>
83 /// <returns></returns>
84 public ActionResult HttpUnauthorizedResultDemo()
85 {
86 return new HttpUnauthorizedResult();
87 }
88
89 /// <summary>
90 /// HttpStatusCodeResult的方法(返回错误状态信息)
91 /// http://localhost:30735/home/HttpStatusCodeResult
92 /// </summary>
93 /// <returns></returns>
94 public ActionResult HttpStatusCodeResultDemo() {
95 return new HttpStatusCodeResult(500, "System Error");
96 }
97
98 /// <summary>
99 /// HttpNotFoundResult的使用方法
100 /// http://localhost:30735/home/HttpNotFoundResultDemo
101 /// </summary>
102 /// <returns></returns>
103 public ActionResult HttpNotFoundResultDemo() {
104 return new HttpNotFoundResult("not found action");
105 }
106
107 /// <summary>
108 /// JavaScriptResult 的用法(返回脚本文件)
109 /// http://localhost:30735/home/JavaScriptResultDemo
110 /// </summary>
111 /// <returns>返回脚本内容</returns>
112 public ActionResult JavaScriptResultDemo()
113 {
114 return JavaScript(@"<script>alert('Test JavaScriptResultDemo!')</script>");
115 }
116
117 /// <summary>
118 /// JsonResult的用法(返回一个json对象)
119 /// http://localhost:30735/home/JsonResultDemo
120 /// </summary>
121 /// <returns>返回一个json对象</returns>
122 public ActionResult JsonResultDemo()
123 {
124 var tempObj = new { Controller = "HomeController", Action = "JsonResultDemo" };
125 return Json(tempObj);
126 }
127
128 /// <summary>
129 /// RedirectResult的用法(跳转url地址)
130 /// http://localhost:30735/home/RedirectResultDemo
131 /// </summary>
132 /// <returns></returns>
133 public ActionResult RedirectResultDemo()
134 {
135 return Redirect(@"http://wwww.baidu.com");
136 }
137
138 /// <summary>
139 /// RedirectToRouteResult的用法(跳转的action名称)
140 /// http://localhost:30735/home/RedirectToRouteResultDemo
141 /// </summary>
142 /// <returns></returns>
143 public ActionResult RedirectToRouteResultDemo()
144 {
145 return RedirectToAction(@"FileStreamResultDemo");
146 }
147
148 /// <summary>
149 /// PartialViewResult的用法(返回部分视图)
150 /// http://localhost:30735/home/PartialViewResultDemo
151 /// </summary>
152 /// <returns></returns>
153 public PartialViewResult PartialViewResultDemo()
154 {
155 return PartialView();
156 }
157
158 /// <summary>
159 /// ViewResult的用法(返回视图)
160 /// http://localhost:30735/home/ViewResultDemo
161 /// </summary>
162 /// <returns></returns>
163 public ActionResult ViewResultDemo()
164 {
165 //如果没有传入View名称, 默认寻找与Action名称相同的View页面.
166 return View();
167 }
168 }
169 }

文章转载自:https://www.cnblogs.com/xielong/p/5940535.html

.Net Mvc学习——ASP.NET MVC中常用的ActionResult类型的更多相关文章

  1. ASP.NET MVC中常用的ActionResult类型

    常见的ActionResult 1.ViewResult 表示一个视图结果,它根据视图模板产生应答内容.对应得Controller方法为View. 2.PartialViewResult 表示一个部分 ...

  2. 学习ASP.NET MVC(十一)——分页

    在这一篇文章中,我们将学习如何在MVC页面中实现分页的方法.分页功能是一个非常实用,常用的功能,当数据量过多的时候,必然要使用分页.在今天这篇文章中,我们学习如果在MVC页面中使用PagedList. ...

  3. 学习ASP.NET MVC(九)——“Code First Migrations ”工具使用示例

    在上一篇文章中,我们学习了如何使用实体框架的“Code First Migrations ”工具,使用其中的“迁移”功能对模型类进行一些修改,同时同步更新对应数据库的表结构. 在本文章中,我们将使用“ ...

  4. 学习ASP.NET MVC(七)——我的第一个ASP.NET MVC 查询页面

    在本篇文章中,我将添加一个新的查询页面(SearchIndex),可以按书籍的种类或名称来进行查询.这个新页面的网址是http://localhost:36878/Book/ SearchIndex. ...

  5. 学习ASP.NET MVC(一)——我的第一个ASP.NET MVC应用程序

    学习ASP.NET MVC系列: 学习ASP.NET MVC(一)——我的第一个ASP.NET MVC应用程序 学习ASP.NET MVC(二)——我的第一个ASP.NET MVC 控制器 学习ASP ...

  6. 七天来学习ASP.NET MVC (两)——ASP.NET MVC 数据传输

    通过第一天的学习之后,我们相信您已经对MVC有一些基本了解. 本节所讲的内容是在上节的基础之上.因此须要确保您是否掌握了上一节的内容. 本章的目标是在今天学习结束时利用最佳实践解决方式创建一个小型的M ...

  7. 从零开始学习ASP.NET MVC 1.0

    转自:http://www.cnblogs.com/zhangziqiu/archive/2009/02/27/ASPNET-MVC-1.html <从零开始学习ASP.NET MVC 1.0& ...

  8. 返璞归真 asp.net mvc (11) - asp.net mvc 4.0 新特性之自宿主 Web API, 在 WebForm 中提供 Web API, 通过 Web API 上传文件, .net 4.5 带来的更方便的异步操作

    原文:返璞归真 asp.net mvc (11) - asp.net mvc 4.0 新特性之自宿主 Web API, 在 WebForm 中提供 Web API, 通过 Web API 上传文件, ...

  9. 系列文章--从零开始学习ASP.NET MVC 1.0

    从零开始学习ASP.NET MVC 1.0 (一) 开天辟地入门篇 从零开始学习 ASP.NET MVC 1.0 (二) 识别URL的Routing组件 从零开始学习 ASP.NET MVC 1.0 ...

随机推荐

  1. 三年之久的 etcd3 数据不一致 bug 分析

    问题背景 诡异的 K8S 滚动更新异常 笔者某天收到同事反馈,测试环境中 K8S 集群进行滚动更新发布时未生效.通过 kube-apiserver 查看发现,对应的 Deployment 版本已经是最 ...

  2. three.js学习3_相机相关

    Three.Camera Camera是所有相机的抽象基类, 在构建新摄像机时,应始终继承此类. 常见的相机有两种类型: PerspectiveCamera(透视摄像机)或者 Orthographic ...

  3. 刷题[b01lers2020]Life on Mars

    解题思路 打开网站,检查常见的信息泄露,漏洞扫描等,都无hint.这时候有点难办了,又找了一会儿,发现抓包标签时,get的值会有参数 尝试访问,发现有如下内容: 因为实在其他地方找不到任何思路了,看着 ...

  4. Spring系列之aAOP AOP是什么?+xml方式实现aop+注解方式实现aop

    Spring系列之aop aop是什么?+xml方式实现aop+注解方式实现aop 什么是AOP? AOP为Aspect Oriented Programming 的缩写,意识为面向切面的编程,是通过 ...

  5. Mac更换鼠标指针样式_mousecape教程

    mousecape项目介绍 这是github上的一个项目,作者是alexzielenski. 项目是用于修改Mac系统鼠标样式的,支持动态鼠标样式. 该项目停止更新于2014年,目前仍可以被较新的系统 ...

  6. python的快捷键

    常用快捷键 1.Ctrl + Enter:在下方新建行但不移动光标 2.Shift + Enter:在下方新建行并移到新行行首 3.Ctrl + /:注释(取消注释)选择的行 4.Ctrl + Alt ...

  7. 在Kubernetes上部署应用时我们常忽略的几件事

    根据我的经验,大多数人(使用Helm或手动yaml)将应用程序部署到Kubernetes上,然后认为他们就可以一直稳定运行. 然而并非如此,实际使用过程还是遇到了一些"陷阱",我希 ...

  8. ZooKeeper 【不仅仅是注册中心,你还知道有哪些?】

    什么是 ZooKeeper Apache ZooKeeper 是一个开源的实现高可用的分布式协调服务器.ZooKeeper是一种集中式服务,用于维护配置信息,域名服务,提供分布式同步和集群管理.所有这 ...

  9. 【原创】xenomai内核解析--实时IPC概述

    版权声明:本文为本文为博主原创文章,转载请注明出处.如有问题,欢迎指正.博客地址:https://www.cnblogs.com/wsg1100/ 目录 1.概述 2.Real-time IPC 2. ...

  10. 再解决不了前端加密我就吃shi

    参考文章 快速定位前端加密方法 渗透测试-前端加密测试 前言 最近学习挖洞以来,碰到数据做了加密基本上也就放弃了.但是发现越来越多的网站都开始做前端加密了,不论是金融行业还是其他.所以趁此机会来捣鼓一 ...