asp.net WebApi WebApiConfig.cs Web API 配置和服務


 

public static void Register(HttpConfiguration config)
{

...............................

var jsonSettings = config.Formatters.JsonFormatter.SerializerSettings;
//獲取或設置在序列化和反序列化期間如何處理空值。
jsonSettings.NullValueHandling = NullValueHandling.Ignore;//在序列化和反序列化對象時忽略空值。

// 移除XML序列化器
config.Formatters.Remove(config.Formatters.XmlFormatter);
config.Formatters.JsonFormatter.Indent = true;
// 解決json序列化時的循環引用問題
config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
// 對 JSON 數據使用混合大小寫。駝峰式,但是是javascript 首字母小寫形式.
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver();
//日期格式
var dateTimeConverter = new Newtonsoft.Json.Converters.IsoDateTimeConverter {
DateTimeFormat = "yyyy-MM-dd HH:mm:dd"
};
config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(dateTimeConverter);

...............................

}


免責聲明!

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



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