.NET MVC5簡介(三)Result

Ajax請求數據響應格式,一個醒目組必須是贊成的,前端才知道怎麼應付,還有不少其餘狀況,好比異常了,有ExceptionFilter,按照固定格式返回,好比沒有權限,Authorization,按照固定格式返回。html

Http請求的本質:前端

  請求--應答式,響應能夠那麼豐富?不一樣的類型其實方式同樣的,只不過有個ContenetType的差異,html--text/html、json--application/json、xml/application/xml、js--application/JavaScript、ico--image/x-icon、image/gif、image/jepg、image/png.....json

  這個等因而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/commonsapp

/// <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));
    }
}

調用的時候:ide

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

AjaxResult:this

 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

自定義擴展XMLResultspa

 /// <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"
        }));
}

相關文章
相關標籤/搜索