WebApi Json.NET custom date handling

前端 未结 2 1405
北海茫月
北海茫月 2020-12-01 16:47

I have globally explicitly configured my MVC4 app to use the JSON.NET serializer . I know i have the choice of using the ISO standard dates or the old Microsoft date format

相关标签:
2条回答
  • 2020-12-01 17:16

    Change your setting set up code like this:

    JsonMediaTypeFormatter jsonFormatter = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
    JsonSerializerSettings jSettings = new Newtonsoft.Json.JsonSerializerSettings()         
    {
        Formatting = Formatting.Indented,
        DateTimeZoneHandling = DateTimeZoneHandling.Utc
    };
    jSettings.Converters.Add(new MyDateTimeConvertor());
    jsonFormatter.SerializerSettings = jSettings;
    

    In your code you are just changing local variable value.

    0 讨论(0)
  • 2020-12-01 17:19

    Thanks, I was going crazy, this worked very well for me,paste this in the Global.asax.cs

    JsonMediaTypeFormatter jsonFormatter = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
    JsonSerializerSettings jSettings =...
    

    create a class MyDateTimeConvertor, paste this in the class

    public class MyDateTimeConvertor : DateTimeConverterBase
    {
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            return DateTime...
    

    wep api mvc4, web services, custom date format.

    0 讨论(0)
提交回复
热议问题