原文鏈接:https://www.muhanxue.com/essays/2015/01/8623699.html
MVC web api 返回JSON的幾種方式
1、在WebApiConfig的Register中加入以下代碼
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
2、在WebApiConfig的Register中加入以下代碼
config.Formatters.Remove(config.Formatters.XmlFormatter);
3、在WebApiApplication的Application_Start中加入以下代碼
GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
1、在MVC中全局去除時間格式中帶T的問題。
MVC中默認使用Newtonsoft.Json序列化的,所以在WebApiConfig的Register中加入以下代碼即可
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new IsoDateTimeConverter { DateTimeFormat = "yyyy'-'MM'-'dd' 'HH':'mm':'ss" });
2、在webservice中去除時間帶T的問題。
IsoDateTimeConverter timejson = new IsoDateTimeConverter { DateTimeFormat = "yyyy'-'MM'-'dd' 'HH':'mm':'ss" }; //在序列化的時候傳入timejson對象 //如: return JsonConvert.SerializeObject(object, timejson);//object是需要序列化的對象
