NET MVC5 Brief Introduction Result

Ajax request data response format, a striking group must agree, the front-end knows how to deal with, there are many other cases, such as exceptions, ExceptionFilter, according to the fixed format return, such as no permission, Authorization, according to the fixed format return.

The nature of the Http request:

Request-response, can the response be so rich? Different types actually have the same way, except that there is a difference in ContenetType: HTML - text / html, JSON - Application / json, xml/application/xml, JS - Application / JavaScript, ICO - image / X - icon, image/gif, image/jepg, image/png....

This is equivalent to the Http protocol, supported by the Web framework and browsers. In fact, the server tells the browser how to process this data. To Download PDF from the page or to show PDF from the page depends on ContentType, application/pdf, application/octet-stream.

Json Result is actually Json Result, inheriting ActionResult, then rewriting ExecuteResult method, specifying ContentType as application/json, and then serializing Data into strings to write to stream. We can expand at will by simply putting data into response and specifying ContentType.

ContentType control table: http://tool.oschina.net/commons

/// <summary>
/// Return ActionResult--MVC The framework will implement it ExecuteResult Method---
/// </summary>
/// <returns></returns>
public JsonResult JsonResultIn()
{
    return Json(new AjaxResult()
    {
        Result = DoResult.Success,
        DebugMessage = "Here is JsonResult"
    }, JsonRequestBehavior.AllowGet);
}

Let's extend Newton Json Result, for example.

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

When calling:

 /// <summary>
 /// Return ActionResult--MVC The framework will implement it ExecuteResult Method---
 /// </summary>
 /// <returns></returns>
 public NewtonJsonResult JsonResultNewton()
 {
     return new NewtonJsonResult(new AjaxResult()
     {
         Result = DoResult.Success,
         DebugMessage = "Here is 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
 }

Custom Extended XMLResult

 /// <summary>
 /// Custom Extension XML format 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 = "Here is JsonResult"
    });
}

 

Now, instead of writing the return value, we write directly to 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 = "Here is JsonResult"
        }));
}

Tags: ASP.NET JSON xml Javascript

Posted on Tue, 08 Oct 2019 21:05:46 -0700 by Benjigga