最近項目中前臺頁面使用Extjs4.2 ,在後臺ASP.NET MVC4返回的DateTime類型的數據錯返回的DateTime類型的JsonResult的結果中的值是「\/Date(1378446180000)\/此種格式,致使頁面顯示時間不正確(以下圖建立時間列)json
因而經過自定義JsonResult處理了JSON序列化DateTime類型數據,將處理方法貼於此,供須要的朋友交流學習!!!!app
建立CustomResult類,繼承JsonResult, 重寫 ExecuteResult方法,代碼以下:ide
public class CustomResult : JsonResult { public override void ExecuteResult(ControllerContext context) { if (context == null) { throw new ArgumentNullException("context"); } 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) { IsoDateTimeConverter timeFormat = new IsoDateTimeConverter(); timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss"; response.Write(JsonConvert.SerializeObject(this.Data, Newtonsoft.Json.Formatting.Indented, timeFormat)); } } }
建立BaseController類,繼承Controller類,重寫Json方法學習
public class BaseController : Controller { protected override JsonResult Json(object data, string contentType, Encoding contentEncoding) { return new CustomResult { Data = data, ContentType = contentType, ContentEncoding = contentEncoding }; } }
這樣就OK了(對了還要解釋下,在CustomResult類裏用到的序列化是Newtonsoft.Json.Converters),以後針對處理返回帶時間格式的 項目中XXController直接繼承BaseController 就能夠了this
處理完的表格的結果以下spa