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