ASP.NET MVC JsonResult Date Format

前端 未结 25 3252
渐次进展
渐次进展 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:50

    I found this to be the easiest way to change it server side.

    using System.Collections.Generic;
    using System.Web.Mvc;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Converters;
    using Newtonsoft.Json.Serialization;
    
    namespace Website
    {
        /// 
        /// This is like MVC5's JsonResult but it uses CamelCase and date formatting.
        /// 
        public class MyJsonResult : ContentResult
        {
            private static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver(),
                Converters = new List { new StringEnumConverter() }
            };
    
            public FindersJsonResult(object obj)
            {
                this.Content = JsonConvert.SerializeObject(obj, Settings);
                this.ContentType = "application/json";
            }
        }
    }
    

提交回复
热议问题