小编典典

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

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))
};

是否可以像这样设置全局序列化程序设置,或者我遗漏了什么?


阅读 583

收藏
2020-07-27

共1个答案

小编典典

设置JsonConvert.DefaultSettings成功了。

JsonConvert.DefaultSettings = () => new JsonSerializerSettings
{
    Formatting = Formatting.Indented,
    TypeNameHandling = TypeNameHandling.Objects,
    ContractResolver = new CamelCasePropertyNamesContractResolver()
};
2020-07-27