尝试设置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:找不到方法:'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
(降级所有其他参考资料)解决此问题
任何人都已经拥有了吗? 感谢..
答案 0 :(得分:55)
.Net Core 1.0 RTM带有开箱即用的CamelCase格式。这是来自RC2的行为change。但是,如果您需要修改它,请尝试以下代码段:
services.AddMvc()
.AddJsonOptions(opt =>
{
var resolver = opt.SerializerSettings.ContractResolver;
if (resolver != null)
{
var res = resolver as DefaultContractResolver;
res.NamingStrategy = null; // <<!-- this removes the camelcasing
}
});
更多信息here。
对于dotnet core 1.0.1:
services
.AddMvcCore()
.AddJsonFormatters(o => o...);
答案 1 :(得分:1)
我假设你使用的是ASP.Net Core,你应该使用“Microsoft.AspNetCore.Mvc”:
所以替换这个:
"Microsoft.AspNet.Mvc": "6.0.0-rc1-final"
由此:
"Microsoft.AspNetCore.Mvc": "1.0.0-rc2-final"