.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机.中型机等机器在处理海量数据时,其计算能力.内存容量等指标都远远无法达到要求.在大数 ...
随机推荐
- 集合 List和Set
分别向Set集合以及List集合中添加“A”,“a”,“c”,“C”,“a”5个元素,观察重复值“a”能否在List集合以及Set集合中成功添加. List<String> ls=new ...
- 有关 IE11
IE10/IE11 中 99% 的 CSS3 属性不需要写 -ms- 前缀: IE9/IE10/IE11 默认开启 GPU 加速,效果比同期 Chrome 版本(如 35)流畅: IE11 的字体渲染 ...
- OpenGL编程逐步深入(六)平移变换
准备知识 从这一节我们开始接触3D对象各种各样的变换,使其显示在屏幕上看起来有深度的感觉.通常每一种变换都是通过矩阵来实现的,把这些变换矩阵逐个的乘起来,然后用乘积乘以顶点位置.在每个教程中,我们致力 ...
- nodejs 通过 get获取数据修改redis数据
如下代码是没有报错的正确代码 我通过https获取到数据 想用redis set一个键值存储 现在我掉入了回调陷阱res.on 里面接收到的数据是data 里面如果放入 client.on('conn ...
- pip源
阿里云 http://mirrors.aliyun.com/pypi/simple/ 中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/ 豆瓣(do ...
- 把qtdesigner中的ui文件生成py文件 anaconda
无奈,马上实习就要结束了,但是自己的长进才是在stm32方面,虽然对linux有了些接触 但本质上没有任何进展,不能不说这事我的悲哀,在研三的时候却要做别人大二时做的事情 如今又是精力太散,迷上了py ...
- Mooc--五子棋(js)小结
1. canvas是块级元素吗?? 2. css3 box-shadow属性 语法:box-shadow: h-shadow v-shadow blur spread color inset; 注释: ...
- dstat---统计磁盘,CPU,IO,等相关信息
dstat命令是一个用来替换vmstat.iostat.netstat.nfsstat和ifstat这些命令的工具,是一个全能系统信息统计工具.与sysstat相比,dstat拥有一个彩色的界面,在手 ...
- iOS开发系列之四 - UITextView 使用方法小结
// 初始化输入框并设置位置和大小 UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 10, 300, 1 ...
- Linux系统的LOG日志文件及入侵后日志的清除
UNIX网管员主要是靠系统的LOG,来获得入侵的痕迹.当然也有第三方工具记录入侵系统的 痕迹,UNIX系统存放LOG文件,普通位置如下: /usr/adm - 早期版本的UNIX/var/adm - ...