尝试设置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();
    });

但是,一旦添加此内容,我将得到:



我正在使用标准的Microsoft.AspNet.Mvc.Formatters.Json (6.0.0-rc1-final)
编辑:通过安装Newtonsoft.Json 6.0.6(降级所有其他引用)解决了该问题

有人知道了吗?
谢谢..

最佳答案

.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...);

关于c# - JsonSerializerSettings和Asp.Net核心,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35772387/

10-11 01:47