JsonSerializerSettings 和 Asp.Net Core [英] JsonSerializerSettings and Asp.Net Core

查看:28
本文介绍了JsonSerializerSettings 和 Asp.Net Core的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试设置 JsonOutputFormatter 选项:

Trying to set JsonOutputFormatter options:

var jsonFormatter = (JsonOutputFormatter) options.OutputFormatters.FirstOrDefault(f => f is JsonOutputFormatter);
if (jsonFormatter != null)
{
    jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
}

mvcBuilder.AddJsonOptions(jsonOptions =>
    {
        jsonOptions.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
    });

但是一旦我添加了这个,我就会得到:

But as soon as I add this, I get:

MissingMethodException: Method not found: 'Newtonsoft.Json.JsonSerializerSettings Microsoft.AspNet.Mvc.Formatters.JsonOutputFormatter.get_SerializerSettings()'.

MissingMethodException: Method not found: 'Newtonsoft.Json.JsonSerializerSettings Microsoft.AspNet.Mvc.Formatters.JsonOutputFormatter.get_SerializerSettings()'.

我正在使用标准的 Microsoft.AspNet.Mvc.Formatters.Json (6.0.0-rc1-final)

通过安装 Newtonsoft.Json 6.0.6 解决它(它降级了所有其他参考)

Solved it by installing Newtonsoft.Json 6.0.6 (which downgrades all other references)

有人已经收到了吗?谢谢..

Anyone got that already? Thanks..

推荐答案

.Net Core 1.0 RTM 带有开箱即用的 CamelCase 格式.这是一种行为 更改 来自 RC2.但是,如果您需要修改它,请尝试以下代码段:

.Net Core 1.0 RTM comes with CamelCase formatting out-of-the-box. This is a behavior change from RC2. However, if you need to modify it, try this snippet:

services.AddMvc()
        .AddJsonOptions(opt =>
    {
        var resolver  = opt.SerializerSettings.ContractResolver;
        if (resolver != null)
        {
            var res = resolver as DefaultContractResolver;
            res.NamingStrategy = null;  // <<!-- this removes the camelcasing
        }
    });

更多信息 这里.

对于 dotnet 核心 1.0.1:

For dotnet core 1.0.1:

  services
            .AddMvcCore()
            .AddJsonFormatters(o => o...);

这篇关于JsonSerializerSettings 和 Asp.Net Core的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆