MVC web api 返回JSON的幾種方式,Newtonsoft.Json序列化日期時間去T的幾種方式。


原文鏈接: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是需要序列化的對象

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM