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