JsonSerializerSettings and Asp.Net Core



尝试设置 JsonOutputFormatter 选项:

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

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

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解决了它(这会降级所有其他引用)

有人已经知道了吗?谢谢。。

.

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

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:

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

我假设你正在使用 ASP.Net Core,你应该使用"Microsoft.AspNetCore.Mvc":

所以替换这个:

"Microsoft.AspNet.Mvc": "6.0.0-rc1-final"

通过这个:

"Microsoft.AspNetCore.Mvc": "1.0.0-rc2-final"

相关内容

  • 没有找到相关文章

最新更新