我试图在我的中设置全局序列化器设置global.asax。
global.asax
var formatter = GlobalConfiguration.Configuration.Formatters.JsonFormatter; formatter.SerializerSettings = new JsonSerializerSettings { Formatting = Formatting.Indented, TypeNameHandling = TypeNameHandling.Objects, ContractResolver = new CamelCasePropertyNamesContractResolver() };
使用以下代码序列化对象时,是否不使用全局序列化程序设置?
return new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(JsonConvert.SerializeObject(page)) };
是否可以像这样设置全局序列化程序设置,或者我遗漏了什么?
设置JsonConvert.DefaultSettings成功了。
JsonConvert.DefaultSettings
JsonConvert.DefaultSettings = () => new JsonSerializerSettings { Formatting = Formatting.Indented, TypeNameHandling = TypeNameHandling.Objects, ContractResolver = new CamelCasePropertyNamesContractResolver() };