ASP.NET MVC JsonResult Date Format

前端 未结 25 4032
渐次进展
渐次进展 2020-11-21 11:22

I have a controller action that effectively simply returns a JsonResult of my model. So, in my method I have something like the following:

return new JsonRes         


        
25条回答
  •  佛祖请我去吃肉
    2020-11-21 11:59

    I found that creating a new JsonResult and returning that is unsatisfactory - having to replace all calls to return Json(obj) with return new MyJsonResult { Data = obj } is a pain.


    So I figured, why not just hijack the JsonResult using an ActionFilter:

    public class JsonNetFilterAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            if (filterContext.Result is JsonResult == false)
            {
                return;
            }
    
            filterContext.Result = new JsonNetResult(
                (JsonResult)filterContext.Result);
        }
    
        private class JsonNetResult : JsonResult
        {
            public JsonNetResult(JsonResult jsonResult)
            {
                this.ContentEncoding = jsonResult.ContentEncoding;
                this.ContentType = jsonResult.ContentType;
                this.Data = jsonResult.Data;
                this.JsonRequestBehavior = jsonResult.JsonRequestBehavior;
                this.MaxJsonLength = jsonResult.MaxJsonLength;
                this.RecursionLimit = jsonResult.RecursionLimit;
            }
    
            public override void ExecuteResult(ControllerContext context)
            {
                if (context == null)
                {
                    throw new ArgumentNullException("context");
                }
    
                var isMethodGet = string.Equals(
                    context.HttpContext.Request.HttpMethod, 
                    "GET", 
                    StringComparison.OrdinalIgnoreCase);
    
                if (this.JsonRequestBehavior == JsonRequestBehavior.DenyGet
                    && isMethodGet)
                {
                    throw new InvalidOperationException(
                        "GET not allowed! Change JsonRequestBehavior to AllowGet.");
                }
    
                var response = context.HttpContext.Response;
    
                response.ContentType = string.IsNullOrEmpty(this.ContentType) 
                    ? "application/json" 
                    : this.ContentType;
    
                if (this.ContentEncoding != null)
                {
                    response.ContentEncoding = this.ContentEncoding;
                }
    
                if (this.Data != null)
                {
                    response.Write(JsonConvert.SerializeObject(this.Data));
                }
            }
        }
    }
    

    This can be applied to any method returning a JsonResult to use JSON.Net instead:

    [JsonNetFilter]
    public ActionResult GetJson()
    {
        return Json(new { hello = new Date(2015, 03, 09) }, JsonRequestBehavior.AllowGet)
    }
    

    which will respond with

    {"hello":"2015-03-09T00:00:00+00:00"}
    

    as desired!


    You can, if you don't mind calling the is comparison at every request, add this to your FilterConfig:

    // ...
    filters.Add(new JsonNetFilterAttribute());
    

    and all of your JSON will now be serialized with JSON.Net instead of the built-in JavaScriptSerializer.

提交回复
热议问题