.Net中常用的几种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错误信息
看栗子,
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 Controllers
{
public class HomeController : Controller
{
/// <summary>
/// ViewResult的用法(返回视图)
/// </summary>
/// <returns></returns>
public ActionResult ViewResultDemo()
{
//如果没有传入View名称, 默认寻找与Action名称相同的View页面.
return View();
}
/// <summary>
/// PartialViewResult的用法(返回部分视图)
/// </summary>
/// <returns></returns>
public PartialViewResult PartialViewResultDemo()
{
return PartialView();
}
/// <summary>
/// ContentResult用法(返回文本)
/// </summary>
/// <returns>返回文本</returns>
public ActionResult ContentResultDemo(){
string str = "ContentResultDemo!";
return Content(str);
}
/// <summary>
/// EmptyResult的用法(返回空对象)
/// </summary>
/// <returns>返回一个空对象</returns>
public ActionResult EmptyResultDemo (){
return new EmptyResult();
}
/// <summary>
/// FileContentResult的用法(返回图片)
/// </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的用法(返回图片)
/// </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的用法(返回图片)
/// </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错误)
/// </summary>
/// <returns></returns>
public ActionResult HttpUnauthorizedResultDemo()
{
return new HttpUnauthorizedResult();
}
/// <summary>
/// HttpStatusCodeResult的方法(返回错误状态信息)
/// </summary>
/// <returns></returns>
public ActionResult HttpStatusCodeResultDemo() {
return new HttpStatusCodeResult(500, "System Error");
}
/// <summary>
/// HttpNotFoundResult的使用方法
/// </summary>
/// <returns></returns>
public ActionResult HttpNotFoundResultDemo() {
return new HttpNotFoundResult("not found action");
}
/// <summary>
/// JavaScriptResult 的用法(返回脚本文件)
/// </summary>
/// <returns>返回脚本内容</returns>
public ActionResult JavaScriptResultDemo()
{
return JavaScript(@"<script>alert('Test JavaScriptResultDemo!')</script>");
}
/// <summary>
/// JsonResult的用法(返回一个json对象)
/// </summary>
/// <returns>返回一个json对象</returns>
public ActionResult JsonResultDemo()
{
var tempObj = new { Controller = "HomeController", Action = "JsonResultDemo" };
return Json(tempObj);
}
/// <summary>
/// RedirectResult的用法(跳转url地址)
/// </summary>
/// <returns></returns>
public ActionResult RedirectResultDemo()
{
return Redirect(@"http://wwww.baidu.com");
}
/// <summary>
/// RedirectToRouteResult的用法(跳转的action名称)
/// </summary>
/// <returns></returns>
public ActionResult RedirectToRouteResultDemo()
{
return RedirectToAction(@"FileStreamResultDemo");
}
}
}
.Net中常用的几种ActionResult的更多相关文章
- 【原】实时渲染中常用的几种Rendering Path
[原]实时渲染中常用的几种Rendering Path 本文转载请注明出处 —— polobymulberry-博客园 本文为我的图形学大作业的论文部分,介绍了一些Rendering Path,比较简 ...
- C#中常用的几种读取XML文件的方法
1.C#中常用的几种读取XML文件的方法:http://blog.csdn.net/tiemufeng1122/article/details/6723764/
- .NET中常用的几种解析JSON方法
一.基本概念 json是什么? JSON:JavaScript 对象表示法(JavaScript Object Notation). JSON 是一种轻量级的数据交换格式,是存储和交换文本信息的语法. ...
- iOS中常用的四种数据持久化方法简介
iOS中常用的四种数据持久化方法简介 iOS中的数据持久化方式,基本上有以下四种:属性列表.对象归档.SQLite3和Core Data 1.属性列表涉及到的主要类:NSUserDefaults,一般 ...
- 【转载】Python编程中常用的12种基础知识总结
Python编程中常用的12种基础知识总结:正则表达式替换,遍历目录方法,列表按列排序.去重,字典排序,字典.列表.字符串互转,时间对象操作,命令行参数解析(getopt),print 格式化输出,进 ...
- Python编程中常用的12种基础知识总结
原地址:http://blog.jobbole.com/48541/ Python编程中常用的12种基础知识总结:正则表达式替换,遍历目录方法,列表按列排序.去重,字典排序,字典.列表.字符串互转,时 ...
- python中常用的九种数据预处理方法分享
Spyder Ctrl + 4/5: 块注释/块反注释 本文总结的是我们大家在python中常见的数据预处理方法,以下通过sklearn的preprocessing模块来介绍; 1. 标准化(St ...
- 请写出JavaScript中常用的三种事件。
请写出JavaScript中常用的三种事件. 解答: onclick,onblur,onChange
- 本文将介绍“数据计算”环节中常用的三种分布式计算组件——Hadoop、Storm以及Spark。
本文将介绍“数据计算”环节中常用的三种分布式计算组件——Hadoop.Storm以及Spark. 当前的高性能PC机.中型机等机器在处理海量数据时,其计算能力.内存容量等指标都远远无法达到要求.在大数 ...
随机推荐
- User-defined types
We have used many of Python’s built-in types; now we are going to define a new type. As an example, ...
- java9新特性-1-概述
经过4次跳票,历经曲折的java 9 终于终于在2017年9月21日发布. 2.哪些人适合看这套视频? 已经熟悉或熟练运用java 8 及 之前 java 版本的开发人员.科研人员.学生及 ...
- sql的系统关键字的概述
create proc proc_B as SELECT * FROM [ZkbTest].[dbo].[T_ZKB] exec sp_helptext proc_B select * from sy ...
- java 8 , merge()
import java.util.HashMap; import java.util.Map; public class j8merge { public static void main(Strin ...
- UI Framework-1: Native Controls
Native Controls Background Despite the fact that views provides facilities for custom layout, render ...
- Decorator - 利用装饰器武装前端代码
历史 以前做后端时,接触过一点Spring,也是第一次了解DI.IOC等概念,面向切面编程,对于面向对象编程还不怎么熟练的情况下,整个人慌的一批,它的日志记录.数据库配置等都非常方便,不回侵入到业务代 ...
- mysql更改密码与远程管理
set password = ': #在当前用户下更改密码 grant all privileges on *.* to root@"%" identified by " ...
- caioj 1082 动态规划入门(非常规DP6:火车票)
f[i]表示从起点到第i个车站的最小费用 f[i] = min(f[j] + dist(i, j)), j < i 动规中设置起点为0,其他为正无穷 (貌似不用开long long也可以) #i ...
- 安装虚拟机(VM)(一)
原创作品,允许转载,转载时请务必声明作者信息和本声明. https://www.cnblogs.com/zhu520/p/10728248.html 本人小白,有错指出.谢谢! 一:安装虚拟机前奏 ...
- Android实战简易教程-第二十六枪(基于ViewPager实现微信页面切换效果)
1.头部布局文件top.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout x ...