.Net Mvc学习——ASP.NET MVC中常用的ActionResult类型
一、定义
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类型的更多相关文章
- ASP.NET MVC中常用的ActionResult类型
常见的ActionResult 1.ViewResult 表示一个视图结果,它根据视图模板产生应答内容.对应得Controller方法为View. 2.PartialViewResult 表示一个部分 ...
- 学习ASP.NET MVC(十一)——分页
在这一篇文章中,我们将学习如何在MVC页面中实现分页的方法.分页功能是一个非常实用,常用的功能,当数据量过多的时候,必然要使用分页.在今天这篇文章中,我们学习如果在MVC页面中使用PagedList. ...
- 学习ASP.NET MVC(九)——“Code First Migrations ”工具使用示例
在上一篇文章中,我们学习了如何使用实体框架的“Code First Migrations ”工具,使用其中的“迁移”功能对模型类进行一些修改,同时同步更新对应数据库的表结构. 在本文章中,我们将使用“ ...
- 学习ASP.NET MVC(七)——我的第一个ASP.NET MVC 查询页面
在本篇文章中,我将添加一个新的查询页面(SearchIndex),可以按书籍的种类或名称来进行查询.这个新页面的网址是http://localhost:36878/Book/ SearchIndex. ...
- 学习ASP.NET MVC(一)——我的第一个ASP.NET MVC应用程序
学习ASP.NET MVC系列: 学习ASP.NET MVC(一)——我的第一个ASP.NET MVC应用程序 学习ASP.NET MVC(二)——我的第一个ASP.NET MVC 控制器 学习ASP ...
- 七天来学习ASP.NET MVC (两)——ASP.NET MVC 数据传输
通过第一天的学习之后,我们相信您已经对MVC有一些基本了解. 本节所讲的内容是在上节的基础之上.因此须要确保您是否掌握了上一节的内容. 本章的目标是在今天学习结束时利用最佳实践解决方式创建一个小型的M ...
- 从零开始学习ASP.NET MVC 1.0
转自:http://www.cnblogs.com/zhangziqiu/archive/2009/02/27/ASPNET-MVC-1.html <从零开始学习ASP.NET MVC 1.0& ...
- 返璞归真 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 上传文件, ...
- 系列文章--从零开始学习ASP.NET MVC 1.0
从零开始学习ASP.NET MVC 1.0 (一) 开天辟地入门篇 从零开始学习 ASP.NET MVC 1.0 (二) 识别URL的Routing组件 从零开始学习 ASP.NET MVC 1.0 ...
随机推荐
- 部署cobbler服务器
部署cobbler服务器 1.准备环境使用nat或者仅主机模式,不要使用桥接模式,方式获取的IP不是自己的 2. 配置yum源[epel]name=epelenabled=1gpgcheck=0bas ...
- SpringBoot框架:'url' attribute is not specified and no embedded datasource could be configured问题处理
一.问题如下: Description: Failed to configure a DataSource: 'url' attribute is not specified and no em ...
- C# 9.0 新特性预览 - 顶级语句
C# 9.0 新特性预览 - 顶级语句 前言 随着 .NET 5 发布日期的日益临近,其对应的 C# 新版本已确定为 C# 9.0,其中新增加的特性(或语法糖)也已基本锁定,本系列文章将向大家展示它们 ...
- 求第k小的数
题目链接:第k个数 题意:求n个数中第k小的数 题解: //由快速排序算法演变而来的快速选择算法 #include<iostream> using namespace std; const ...
- lua、python对比学习
一.基本数据类型: lua: nil(空).boolean(false和nil为假).number(数值).string(字符串).table(表).function(方法).thread (线程) ...
- 手把手教你在 TKE 集群中实现简单的蓝绿发布和灰度发布
概述 如何在腾讯云 Kubernetes 集群实现蓝绿发布和灰度发布?通常要向集群额外部署其它开源工具来实现,比如 Nginx Ingress,Traefik 等,或者让业务上 Service Mes ...
- Istio 运维实战系列(3):让人头大的『无头服务』-下
本系列文章将介绍用户从 Spring Cloud,Dubbo 等传统微服务框架迁移到 Istio 服务网格时的一些经验,以及在使用 Istio 过程中可能遇到的一些常见问题的解决方法. 失败的 Eur ...
- ARCENGINE 10 开发遇到的一些问题
许多版友在刚刚使用ArcGIS 10做开发的时候,都会遇到这样那样的问题.在担任实习版主的这一个多月里,看到了这么几个与开发环境相关的问题,重复被提到相当多,于是我就做了这个FAQ.Q:哪儿有10的A ...
- 开源发丝分割数据集CelebAHairMask-HQ(国庆献礼)
在这个特别日子里,举国欢庆,什么都可以缺席,大礼包不行. 本次开源针对CelebAMask-HQ中发丝部分进行细化的数据集. 该数据集可用于发丝分割等方向的研究和探索. 在过去的一年时间里,疫情改变很 ...
- 独立看第一个C++程序到最终结果log----2019-04-16
(如果一个人夸你,千万别相信,一个人真优秀是不需要说出来的,所以别人夸你的时候也是自己最松懈的时候,千万不能飘,只能说明自己不是很差而已,世界上优秀的人很多,一直优秀到最后的人却是凤毛菱角. 如果一个 ...