设置默认的全局JSON序列化器设置

我试图在我的中设置全局序列化器设置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 = () => new JsonSerializerSettings

{

Formatting = Formatting.Indented,

TypeNameHandling = TypeNameHandling.Objects,

ContractResolver = new CamelCasePropertyNamesContractResolver()

};

以上是 设置默认的全局JSON序列化器设置 的全部内容, 来源链接: utcz.com/qa/413466.html

回到顶部