web api解決序列化後返回標準時間帶T問題

添加類:api

public class JsonDataTimeConverter:IsoDateTimeConverter
    {
       public JsonDataTimeConverter()
       {
           DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
       }
        public override object ReadJson(JsonReader reader, Type objecType,object existingValue,JsonSerializer serializer)
       {
            DateTime dataTime;
            if (DateTime.TryParse(reader.Value.ToString(), out dataTime))
            {
                return dataTime;
            }
            else
            {
                return existingValue;
            }
        }
    }

在Global.asax 中Application_Start 方法中調用ide

   GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.Converters.Insert(0,new JsonDataTimeConverter());orm

 

 JsonDataTimeConverter類經過DateTime.TryParse判斷時間參數,能夠解決在api入參請求中時間參數不完整致使時間爲null的問題io

相關文章
相關標籤/搜索