.Net MVC Json 日期格式


.Net MVC 默認json序列化器是JavaScriptSerializer, 這個序列化工具序列化DateTime 類型會返回一個基於Microsoft特定日期格式的字符串,比如:/Date(1457085759430)/

甚是難看!

 

於是自己寫了個繼承JsonResult 的新類,修改序列化器為NewtonSoft或者其他,從此日期格式嗖嗖的清爽!哇哈哈哈。。。

代碼很簡單:

    public class MyJsonResult:JsonResult
    {
        public override void ExecuteResult(ControllerContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if (this.JsonRequestBehavior == JsonRequestBehavior.DenyGet && string.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
            {
                throw new InvalidOperationException("JsonRequest_GetNotAllowed");
            }
            HttpResponseBase response = context.HttpContext.Response;
            if (!string.IsNullOrEmpty(this.ContentType))
            {
                response.ContentType = this.ContentType;
            }
            else
            {
                response.ContentType = "application/json";
            }
            if (this.ContentEncoding != null)
            {
                response.ContentEncoding = this.ContentEncoding;
            }
            if (this.Data != null)
            {
                response.Write(JsonSerializer.SerializeToString(this.Data));
            }
        }
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM