ActionResult是控制器方法执行后返回的结果类型,控制器方法可以返回一个直接或间接从ActionResult抽象类继承的类型,如果返回的是非ActionResult类型,控制器将会将结果转换为一个ContentResult类型。默认的ControllerActionInvoker调用ActionResult.ExecuteResult方法生成应答结果。
MVC中实现的默认ActionResult如下:

1、ContentResult:
返回简单的纯文本内容,可通过ContentType属性指定应答文档类型,通过ContentEncoding属性指定应答文档的字符编码。可通过Controller类中的Content方法便捷地返回ContentResult对象。如果控制器方法返回非ActionResult对象,MVC将简单地以返回对象的ToString()内容为基础产生一个ContentResult对象。
2、EmptyResult: 返回一个空的结果。如果控制器方法返回一个null,MVC将其转换成EmptyResult对象。
3、RedirectResult: 表示一个连接跳转,相当于ASP.NET中的Response.Redirect方法。对应的Controller方法为Redirect。
4、RedirectToRouteResult:同样表示一个调转,MVC会根据我们指定的路由名称或路由信息(RouteValueDictionary)来生成Url地址,然后调用Response.Redirect跳转。对应的Controller方法为RedirectToAction和RedirectToRoute。
5、ViewResult: 表示一个视图结果,它根据视图模板产生应答内容。对应Controller方法为View。
6、PartialViewResult: 表示一个部分视图结果,与ViewResult本质上一致,只是部分视图不支持母版,对应于ASP.Net,ViewResult相当于一个Page,而PartialViewResult则相当于一个UserControl。它对应的Controller方法为PartialView。
7、HttpUnauthorizedResult: 表示一个未经授权访问的错误。MVC会向客户端发送一个401的应答状态。如果在web.config中开启了表单验证(authentication mode="Forms"),则401状态会将Url转向指定的loginUrl链接。
8、JavaScriptResult: 本质上是一个文本内容,只是将Response.ContentType设置为 application/x-JavaScript,此结果应该和MicrosoftMvcAjax.js脚本配合使用,客户端接收到Ajax应答后,将判断Response.ContentType的值,如果是application/x-javascript,则直接eval执行返回的应答内容。此结果类型对应的Controller方法为JavaScript。
9、JsonResult: 表示一个JSON结果。MVC将Response.ContentType设置为application/json,并通过JavaScriptSerializer类将指定对象序列化为Json表示方式。需要注意,默认情况下,MVC不允许GET请求返回JSON结果,要解除此限制,在生成JsonResult对象时,将其JsonRequestBehavior属性设置为JsonRequestBehavior.AllowGet。此结果对应的Controller方法为Json。
10、FilePathResult、FileContentResult、FileStreamResult: 这三个类继承于FileResult,表示一个文件内容,三者的区别在于,FilePath通过路径传送文件到客户端,FileContent通过二进制数据的方式,而FileStream是通过Stream的方式来传送。Controller为这三个文件结果类型提供了一个名为File的重载方法。
通过直接或间接地从ActionResult继承,可实现自定义的结果类型,下例将实现一个XmlResult类型,用于返回XML应答内容:
1、创建一个空的MVC项目
2、实现XmlResult类
-
public class XmlResult : ActionResult
-
{
-
public XmlResult(Object data)
-
{
-
this.Data = data;
-
}
-
public Object Data
-
{
-
get;
-
set;
-
}
-
public override void ExecuteResult(ControllerContext context)
-
{
-
if (Data == null)
-
{
-
new EmptyResult().ExecuteResult(context);
-
return;
-
}
-
context.HttpContext.Response.ContentType = "application/xml";
-
using (MemoryStream ms = new MemoryStream())
-
{
-
XmlSerializer xs = new XmlSerializer(Data.GetType());
-
xs.Serialize(ms, Data);
-
ms.Position = 0;
-
using (StreamReader sr = new StreamReader(ms))
-
{
-
context.HttpContext.Response.Output.Write(sr.ReadToEnd());
-
}
-
}
-
}
-
}
3、创建一个HomeController,实现Index方法
-
public ActionResult Index()
-
{
-
return new XmlResult(new Product()
-
{
-
ID = "000001",
-
Name = "测a试?",
-
Description = ""
-
});
-
}
-
-
/// <summary>
-
/// This code was found here:
-
/// http://blogs.msdn.com/b/miah/archive/2008/11/13/extending-mvc-returning-an-image-from-a-controller-action.aspx
-
/// </summary>
-
public class ImageResult : ActionResult
-
{
-
public ImageResult(Stream imageStream, string contentType)
-
{
-
if (imageStream == null)
-
throw new ArgumentNullException("imageStream");
-
if (contentType == null)
-
throw new ArgumentNullException("contentType");
-
-
this.ImageStream = imageStream;
-
this.ContentType = contentType;
-
}
-
-
public Stream ImageStream { get; private set; }
-
public string ContentType { get; private set; }
-
-
public override void ExecuteResult(ControllerContext context)
-
{
-
if (context == null)
-
throw new ArgumentNullException("context");
-
-
HttpResponseBase response = context.HttpContext.Response;
-
-
response.ContentType = this.ContentType;
-
-
byte[] buffer = new byte[4096];
-
while (true)
-
{
-
int read = this.ImageStream.Read(buffer, 0, buffer.Length);
-
if (read == 0)
-
break;
-
-
response.OutputStream.Write(buffer, 0, read);
-
}
-
-
response.End();
-
}
-
}
ASP.NET MVC
实现AJAX跨域请求的两种方法
通常发送AJAX请求都是在本域内完成的,也就是向本域内的某个URL发送请求,完成部分页面的刷新。但有的时候需要向其它域发送AJAX请求,完成数据的加载,例如Google。
在ASP.Net MVC
框架里实现跨域的AJAX请求有几种方式可以实现,以下就介绍常用的两种方法。
1. 发送JSONP请求
客户端:
jQuery对发送JSONP请求有很好的支持,客户端通过.
ajax() 函数发送请求,其中需要制定 dataType 为“jsonp” jsonpCallback 为指定的回调函数名(如 “UpdateDiv”),也就是客户端需要定义一个UpdateDiv 函数,当请求成功后会自动调用该函数。
服务器:
ASP.NET MVC没有内置对 JSONP 请求的支持,不过使用 JsonResult 可以很方便的实现。我们只需要定义一个类(如 JsonpResult)继承自JsonResult,并重写 ExecuteResult()方法,在输出数据时,调用客户端定义的那个回调函数(这个函数必须是存在的,并且必须是全局和唯一的)。需要注意的是,在自定义的JsonpResult 里,需要设置 JsonRequestBehavior 为 AllowGet,否则会出错,因为 JSONP 请求必须是 Get 方式提交的。
客户端代码:
-
<scripttype="text/JavaScript">
-
functionUpdateDiv(result) {
-
$("#div1").html(result.ID +result.Name);
-
}
-
$(function() {
-
$(".btn").click(function () {
-
$.ajax({
-
type: "GET",
-
-
url:"http://localhost:50863/Home/Index2", //跨域url
-
dataType: "jsonp",
-
//指定 jsonp 请求
-
jsonpCallback: "UpdateDiv" // 指定回调函数
-
-
});
-
})
-
})
-
</script>
服务端:
-
public class JSONPResult :JsonResult
-
-
{
-
-
public JSONPResult()
-
-
{
-
-
JsonRequestBehavior=JsonRequestBehavior.AllowGet;
-
-
}
-
-
-
-
public string Callback{get;set;}
-
-
-
-
///<summary>
-
-
///对操作结果进行处理
-
-
///</summary>
-
-
///<paramname="context"></param>
-
-
public override void ExecuteResult(ControllerContext context)
-
-
{
-
-
var httpContext = context.HttpContext;
-
-
var callBack = Callback;
-
-
-
-
if(string.IsNullOrWhiteSpace(callBack))
-
-
callBack = httpContext.Request["callback"]; //获得客户端提交的回调函数名称
-
-
-
-
// 返回客户端定义的回调函数
-
-
httpContext.Response.Write(callBack +"(");
-
-
httpContext.Response.Write(Data); //Data 是服务器返回的数据
-
-
httpContext.Response.Write(");"); //将函数输出给客户端,由客户端执行
-
-
}
-
-
-
-
}
-
-
//操作器和其它操作没什么区别,只是返回值是JsopnpResult结果
-
-
public ActionResult Index2()
-
-
{
-
-
var str = "{ID :'123', Name : 'asdsa' }";
-
-
return new JSONPResult{Data = str }; //返回 jsonp 数据,输出回调函数
-
-
}
2. 跨域资源共享
相比 JSONP 请求,跨域资源共享要简单许多,也是实现跨域 AJAX 请求的首选。
客户端:
客户端不在发送 JSONP 类型的请求,只需要发送普通的 JSON 请求即可,也不用定义回调函数,用 .success 即可。
服务端:
服务端也很简单,操作结果还是返回普通的操作结果就可以,唯一要指定的是 HTTP 报文头部的Access-Control-Allow-Origi
指定为 “*” 即可,表示该输出允许跨域实现。
跨域资源共享可以很方便的实现,不过在 IE9 还没有对该技术的支持,FireFox 就已经支持了。
代码:
客户端:
-
<scripttype="text/javascript">
-
-
$(function() {
-
-
$(".btn").click(function (){
-
-
$.ajax({
-
-
type:"GET",
-
-
url:"http://localhost:50863/Home/Index3", //跨域URL
-
-
dataType:"json",
-
-
success:function (result){
-
-
$("#div1").html(result.ID +result.Name);
-
-
},
-
-
error:function (XMLHttpRequest, textStatus,errorThrown) {
-
-
alert(errorThrown); // 调用本次AJAX请求时传递的options参数
-
-
}
-
-
});
-
-
})
-
-
})
-
-
</script>
服务端:
-
///<summary>
-
-
///跨站资源共享实现跨站AJAX请求
-
-
///</summary>
-
-
///<returns></returns>
-
-
public ActionResult Index3()
-
-
{
-
-
var str = new { ID="123", Name= "asdsa" };
-
-
HttpContext.Response.AppendHeader("Access-Control-Allow-Origin","*");
-
-
return Json(str, JsonRequestBehavior.AllowGet);
-
-
}
3.
-
public class JsonpResult : JsonResult
-
{
-
private static readonly string JsonpCallbackName = "callback";
-
private static readonly string CallbackApplicationType = "application/json";
-
-
/// <summary>
-
/// Enables processing of the result of an action method by a custom type that inherits from the <see cref="T:System.Web.Mvc.ActionResult"/> class.
-
/// </summary>
-
/// <param name="context">The context within which the result is executed.</param>
-
/// <exception cref="T:System.ArgumentNullException">The <paramref name="context"/> parameter is null.</exception>
-
public override void ExecuteResult(ControllerContext context)
-
{
-
if (context == null)
-
{
-
throw new ArgumentNullException("context");
-
}
-
if ((JsonRequestBehavior == JsonRequestBehavior.DenyGet) &&
-
String.Equals(context.HttpContext.Request.HttpMethod, "GET"))
-
{
-
throw new InvalidOperationException();
-
}
-
var response = context.HttpContext.Response;
-
if (!String.IsNullOrEmpty(ContentType))
-
response.ContentType = ContentType;
-
else
-
response.ContentType = CallbackApplicationType;
-
if (ContentEncoding != null)
-
response.ContentEncoding = this.ContentEncoding;
-
if (Data != null)
-
{
-
String buffer;
-
var request = context.HttpContext.Request;
-
var serializer = new JavaScriptSerializer();
-
if (request[JsonpCallbackName] != null)
-
buffer = String.Format("{0}({1})", request[JsonpCallbackName], serializer.Serialize(Data));
-
else
-
buffer = serializer.Serialize(Data);
-
response.Write(buffer);
-
}
-
}
-
}
-
-
public static class ContollerExtensions
-
{
-
/// <summary>
-
/// Extension methods for the controller to allow jsonp.
-
/// </summary>
-
/// <param name="controller">The controller.</param>
-
/// <param name="data">The data.</param>
-
/// <returns></returns>
-
public static JsonpResult Jsonp(this Controller controller, object data)
-
{
-
JsonpResult result = new JsonpResult()
-
{
-
Data = data,
-
JsonRequestBehavior = JsonRequestBehavior.AllowGet
-
};
-
-
return result;
-
}
-
}