I'm trying to set the global serializer settings like this in my 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))
};

Isn't it possible to set the global serializer settings like this or am I missing something?

推荐答案

Setting the JsonConvert.DefaultSettings did the trick.

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

Json相关问答推荐

JOLT转换,将属性复制到同级别的dict中

带有PowerShell内核的NewtonSoft json.Net的奇怪行为

使用更高级别架构中的字段值在$def内实现约束

将数组中的值作为键连接到另一个数组中的值(Jolt)

如何在 Apache NiFi 中使用 JoltTransformJson 删除流文件或具有空字段的整个对象?

jq可以在两个JSON对象列表中依次添加对象吗?

JOLT 在 struct 体中间添加一个 JSON 字段

shell解析json并循环输出组合变量

如何在 Postman 中匹配 json 响应中的内容?并可视化

如何使用 React 从 NASA IMAGES API 中解构所需信息

错误字符串的长度超过了maxJsonLength属性设置的值

如何找出实际安装了哪个版本的 bower 包?

应该使用什么标头将 GZIP 压缩 JSON 从 Android 客户端发送到服务器?

在 JSON API Wordpress 上启用 CORS

如何使用 C# 将 JSON 文本转换为对象

如何使用 Jackson 重命名 JSON 序列化中的根键

从 VS 2017 Azure Function 开发中的 local.settings.json 读取值

有什么方法可以判断您安装的 gulp 版本吗?

js 中奇怪的 JSON 解析行为,Unexpected token :

Newtonsoft 对象 → 获取 JSON 字符串