JsonSerializerSettings和Asp.Net核心 [英] JsonSerializerSettings and 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:找不到方法:' 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 core 1.0.1:
For dotnet core 1.0.1:
services
.AddMvcCore()
.AddJsonFormatters(o => o...);
这篇关于JsonSerializerSettings和Asp.Net核心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!