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

 

posted @ 2016-08-25 12:32  culnoty  阅读(2287)  评论(0编辑  收藏  举报