一、定义

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. DVWA sql注入low级别

    DVWA sql注入low级别 sql注入分类 数字型注入 SELECT first_name, last_name FROM users WHERE user_id = $id 字符型注入 SELE ...

  2. 使用Mysql分区表对数据库进行优化

    早期工作中没有做好足够的设计,目前记录表单表数据2000w且无有效索引,表现是分页缓慢,模糊查询拉闸. 当前业务中,写操作会多于读操作,时不时会遇到慢SQL占用过多的数据连接,导致写操作无法正常进行. ...

  3. tomact在windows系统下安装

    一.下载 下载地址: https://tomcat.apache.org/download-90.cgi 7,8,9的版本都可以下,这里下载最新版本 注意:Binary是编译好的,可以直接使用的版本, ...

  4. 6.Exchanger-交换机

  5. idea快捷键壁纸

  6. 4.Scala语法02 - 函数

  7. 手写“SpringBoot”近况:IoC模块已经完成

    jsoncat:https://github.com/Snailclimb/jsoncat (About 仿 Spring Boot 但不同于 Spring Boot 的一个轻量级的 HTTP 框架) ...

  8. Java知识系统回顾整理01基础06数组01创建数组

    一.数组定义 定义:数组是一个固定长度的,包含了相同类型数据的 容器 二.声明数组 int[] a; 声明了一个数组变量. []表示该变量是一个数组 int 表示数组里的每一个元素都是一个整数 a 是 ...

  9. C++实现串口通信问题(与Arduino)

    参考1(已验证稍加修改可与Arduino通信):https://blog.csdn.net/qq_36106219/article/details/81701368 参考2(比较全,main函数需要自 ...

  10. Ubuntu通过iptables防止ssh暴力破解

    步骤 安装iptables-persistent用于保存iptables规则 配置iptables规则 实时更新iptables规则以拦截IP访问 安装iptables-persistent sudo ...