MVC web api returns several ways of JSON, Newtonsoft.Json date and time sequence of several ways to T

MVC web api returns several ways of JSON, Newtonsoft.Json date and time sequence of several ways to T

2015-01-18 00:11 https://www.muhanxue.com/essays/2015/01/8623699.html

MVC web api return JSON in several ways

1, in  WebApiConfigthe  Registerthe following code is added

config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));

2, in  WebApiConfigthe  Registeradding the following codes

config.Formatters.Remove(config.Formatters.XmlFormatter);

3, in  WebApiApplicationthe  Application_Startadd the following code

GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();

In MVC global problem of removing time format tape T.

1, MVC Newtonsoft.Json using default serialization, so in  WebApiConfigthe  Registeradding the following code can be

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new IsoDateTimeConverter
{
    DateTimeFormat = "yyyy'-'MM'-'dd' 'HH':'mm':'ss"
});

2, in  webservicethe problem of removing the tape T time.

IsoDateTimeConverter timejson = new IsoDateTimeConverter
{
    DateTimeFormat = "yyyy'-'MM'-'dd' 'HH':'mm':'ss"
};
//在序列化的时候传入timejson对象
return JsonConvert.SerializeObject(object, timejson); // object是需要序列化的对象

Guess you like

Origin www.cnblogs.com/Jeely/p/10959008.html