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 ...
随机推荐
- IO之Socket网络编程
一.Socket Socket不是Java中独有的概念,而是一个语言无关标准.任何可以实现网络编程的编程语言都有Socket. 1,Socket概念 网络上的两个程序通过一个双向的通信连接实现数据的交 ...
- gis空间分析案例教程-下篇预告
1. 三维空间坐标转换,地理处理工具 2. 3参数,7参数计算,地理处理工具 3. 3参数,7参数坐标转换,地理处理工具 4. 坐标转换工具箱:集成高斯投影,参数计算,坐标转换所有功能. 作业:GIS ...
- JS pc端和移动端共同实现复制到剪贴板功能实现
JS pc端和移动端实现复制到剪贴板功能实现 在网页上复制文本到剪切板,一般是使用JS+Flash结合的方法,网上有很多相关文章介绍.随着 HTML5 技术的发展,Flash 已经在很多场合不适用了, ...
- osgViewer::Viewer::Windows
osg自带窗口去掉边框 #ifdef _WIN32 #include <Windows.h> #endif // _WIN32 #include<iostream> #incl ...
- 阶段5 3.微服务项目【学成在线】_day09 课程预览 Eureka Feign_11-课程详情页面静态化-课程信息模板设计
测试模板 页面的模板已经做好了 直接那来用 测试模板,把这个模板copy到这个测试freemarker的resources目录 远程调用 获取数据 返回模板的文件名 tomcat不支持ssr.ngin ...
- java如何生成 MD5?
java生成 MD5最简单方法 答: 复制以下方法即可使用,不用依赖任何第三方包 public static String MD5(String data) throws Exception { ...
- Hibernatne 缓存中二级缓存简单介绍
hibernate的session提供了一级缓存,每个session,对同一个id进行两次load,不会发送两条sql给数据库,但是session关闭的时候,一级缓存就失效了. 二级缓存是Sessio ...
- 《CNCF × Alibaba云原生技术公开课》知识点自测(二):容器基本概念
(单选)1.已运行 docker run -d -t —name demo ubuntu top 和 docker run --name demo-x --pid container:demo ubu ...
- 解决安装eclipse时出现"Failed to load JNI shared library"
下午远程帮别人弄了很久的eclipse,安装时老是出现如图的字样 最后在安装配置文件中找到问题所在,这个最新版本的eclipse需要jdk1.8的环境,由于系统的jdk是1.7,版本过低从而导致安装失 ...
- 【ARM-Linux开发】Rico Board上编译USB WiFi RT3070驱动
1.附件中提供了RT3070驱动源码包DPO_RT5572_LinuxSTA_2.6.1.3_20121022.tar.gz和编译好的驱动,这里选择使用taget_file.tar.gz中已经编译好的 ...