.NET MVC5简介(三)Result

Ajax请求数据响应格式,一个醒目组必须是同意的,前端才知道怎么应付,还有很多其他情况,比如异常了,有ExceptionFilter,按照固定格式返回,比如没有权限,Authorization,按照固定格式返回。

Http请求的本质:

  请求--应答式,响应可以那么丰富?不同的类型其实方式一样的,只不过有个ContenetType的差别,html--text/html、json--application/json、xml/application/xml、js--application/JavaScript、ico--image/x-icon、image/gif、image/jepg、image/png.....

  这个等于是Http协议约定,Web框架和浏览器共同支持的。其实就是服务器告诉浏览器如何处理这个数据,从页面下载pdf或者页面展示pdf靠的就是ContentType,application/pdf、application/octet-stream。

MVC各种Result的事,JsonResult实际上就是JsonResult,继承ActionResult,然后重写ExecuteResult方法,指定ContentType为application/json,然后将Data序列化成字符串写入stream,我们可以随意扩展,只需要把数据放到response,指定好ContentType

ContentType对照表:http://tool.oschina.net/commons

/// <summary>
/// 返回ActionResult--MVC框架会执行其ExecuteResult方法---
/// </summary>
/// <returns></returns>
public JsonResult JsonResultIn()
{
    return Json(new AjaxResult()
    {
        Result = DoResult.Success,
        DebugMessage = "这里是JsonResult"
    }, JsonRequestBehavior.AllowGet);
}

比如我们现在来扩展个NewtonJsonResult

public class NewtonJsonResult : ActionResult
{
    private object Data = null;
    public NewtonJsonResult(object data)
    {
        this.Data = data;
    }
    public override void ExecuteResult(ControllerContext context)
    {
        HttpResponseBase response = context.HttpContext.Response;
        response.ContentType = "application/json";
        response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(this.Data));
    }
}

调用的时候:

 /// <summary>
 /// 返回ActionResult--MVC框架会执行其ExecuteResult方法---
 /// </summary>
 /// <returns></returns>
 public NewtonJsonResult JsonResultNewton()
 {
     return new NewtonJsonResult(new AjaxResult()
     {
         Result = DoResult.Success,
         DebugMessage = "这里是JsonResult"
     });
 }

AjaxResult:

 public class AjaxResult
 {
     public AjaxResult()
     { }
     public string DebugMessage { get; set; }
     public string PromptMsg { get; set; }
     public DoResult Result { get; set; }
     public object RetValue { get; set; }
     public object Tag { get; set; }
 }
 public enum DoResult
 {
     Failed = 0,
     Success = 1,
     OverTime = 2,
     NoAuthorization = 3,
     Other = 255
 }
View Code

自定义扩展XMLResult

 /// <summary>
 /// 自定义扩展XML格式result
 /// </summary>
 public class XmlResult : ActionResult
 {
     private object _data;

     public XmlResult(object data)
     {
         _data = data;
     }

     public override void ExecuteResult(ControllerContext context)
     {
         var serializer = new XmlSerializer(_data.GetType());
         var response = context.HttpContext.Response;
         response.ContentType = "text/xml";
         serializer.Serialize(response.Output, _data);
     }
 }
public XmlResult XmlResult()
{
    return new XmlResult(new AjaxResult()
    {
        Result = DoResult.Success,
        DebugMessage = "这里是JsonResult"
    });
}

 

 现在,我们不写返回值,直接写入stream

/// </summary>
public void JsonResultVoid()
{
    HttpResponseBase response = base.HttpContext.Response;
    response.ContentType = "application/json";
    response.Write(
        Newtonsoft.Json.JsonConvert.SerializeObject(new AjaxResult()
        {
            Result = DoResult.Success,
            DebugMessage = "这里是JsonResult"
        }));
}

 

.NET MVC5简介(三)Result

全文结束