using Newtonsoft.Json.Converters; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace dccdc.Common { public class JsonResultDate : JsonResult { const string error = "请设置JsonRequestBehavior AllowGet。"; 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(error); } System.Web.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) { var timeConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd" }; string result = Newtonsoft.Json.JsonConvert.SerializeObject(Data, Newtonsoft.Json.Formatting.Indented, timeConverter); response.Write(result); } } } }