C# .NETCore3.0.100-preview6-APIJSON响应总是camelcase,但我的类不是

C# .NETCore3.0.100-preview6-APIJSON响应总是camelcase,但我的类不是,c#,.net-core,json.net,json-deserialization,json-serialization,C#,.net Core,Json.net,Json Deserialization,Json Serialization,我有一个.NETCore3.0预览版6MVC应用程序和API。 在API中,我使用了一个第三方类库(我无法更改),它将类属性定义为Pascal大小写为JsonProperty,PropertyName snaked大小写为 public class Company { [JsonProperty(PropertyName = "company_name")] public string CompanyName { get; set; } more properties …. } 问题是,当我

我有一个.NETCore3.0预览版6MVC应用程序和API。 在API中,我使用了一个第三方类库(我无法更改),它将类属性定义为Pascal大小写为JsonProperty,PropertyName snaked大小写为

public class Company
{
[JsonProperty(PropertyName = "company_name")]
public string CompanyName { get; set; }

more properties ….
}
问题是,当我通过api提供这些时,它们会以驼峰模式(默认为.net core 3)到达MVC应用程序。。。然后无法反序列化回类模型

不管我怎么做,API总是生成驼峰式JSon,例如,上面的属性将被称为companyName

我试过了

options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver { NamingStrategy = new CamelCaseNamingStrategy { OverrideSpecifiedNames = true } };

options.SerializerSettings.ContractResolver = new DefaultContractResolver { NamingStrategy = new DefaultNamingStrategy { OverrideSpecifiedNames = true } };
我已经在camel和默认ContractResolver上尝试了NamingStrategy=null。还尝试将NamingStrategy设置为Snake

但是输出的Json没有任何改变,它始终是camelcased

通过在MVC应用程序中使用ReadAsStringAsync,我可以看到结果字符串是驼峰大小写的。。。I当我使用JsonConvert.DeserializeObject时,属性始终为null,因为名称或Json PropertyName都与结果字符串中的名称不匹配

这是.net核心预览中的一个bug还是我遗漏了其他东西

感谢Mustafa,您建议的副本与我已经尝试过的解决方案有点相同,即将ContractResolver/NamingStrategy的设置更改为不同的值。。。。然而,我的问题是,所有建议的解决方案似乎都不会对API响应产生任何影响,因为它总是以camelCased的形式返回

有趣的是,当我将NamingStrategy改为Snake时,Swagger将模式显示为set(即Snake),但实际输出仍然是camelCased


此外,我无法控制基类,因此无法更改尝试传输的类的名称/json属性。

尝试此操作并删除所有
JsonProperty
属性

从现在起,如果您不指定任何
JsonProperty
y,它将像
CompanyName
一样执行
CompanyName
ProPertyName1
一样执行
pro\u-perty\u-name1
。在本例中,我们将解释属性名称的概念

请确保将此配置添加到
ConfigureServices
方法的底部,它可能会被其他我不知道的内容覆盖

services.AddMvc().AddJsonOptions(options =>
{
    options.SerializerSettings.ContractResolver = new DefaultContractResolver { NamingStrategy = new SnakeCaseNamingStrategy() };
});

不太确定问题出在哪里,但有一种感觉,这与Newtonsoft.Json、Json.Net、Swagger的混合以及我使用Microsoft.AspNet.WebApi.Client获取HttpContent.ReadAsAsync有关……所有这些都有不同的Json

因此,我决定使用.NETCore预览中包含的新System.Text.Json(而不是其他库),重新开始一个真正简单的应用程序和api。也不使用HttpContent.ReadAsAsync,而是将响应作为字符串读取,然后使用新库(System.Text.Json)反序列化

做这件事我有完全相同的问题…。属性名或Json PropertyName都与api返回字符串中的名称不匹配,即class property name=“CompanyName”和Json PropertyName=“company\u name”以及api提供的Json name=“CompanyName”。因此,反序列化时不设置该值

但是,在新的System.Text.Json选项中,我可以指定propertyNameCaseSensitive=true,这解决了我的问题,现在companyName等于companyName,并且在反序列化时正确设置了类模型值

所以我的api调用方法最终看起来像这样

        using HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, string.Format("Companies?aCompanyName={0}", aCompanyName));
        using HttpResponseMessage response = await Client.SendAsync(request);
        string content = await response.Content.ReadAsStringAsync();
        if (response.IsSuccessStatusCode == false)
        {
            throw new ApiException
            {
                StatusCode = (int)response.StatusCode,
                Content = content
            };
        }
        _JsonOptions = new JsonSerializerOptions
        {
            PropertyNameCaseInsensitive = true
        };
        return JsonSerializer.Deserialize<IEnumerable<Company>> (content, _JsonOptions);
使用HttpRequestMessage request=newhttprequestmessage(HttpMethod.Get,string.Format(“companys?aCompanyName={0}”,aCompanyName));
使用HttpResponseMessage response=wait Client.SendAsync(请求);
string content=wait response.content.ReadAsStringAsync();
if(response.issusccessstatuscode==false)
{
抛出新异常
{
StatusCode=(int)response.StatusCode,
内容=内容
};
}
_JsonOptions=新的JsonSerializerOptions
{
PropertyNameCaseSensitive=true
};
返回JsonSerializer.Deserialize(内容,_JsonOptions);
我确实尝试在startup类中全局设置JsonSerializerOptions,但没有成功

我已经将这种方法转移到我应用程序中的所有http调用中,删除了对Newtonsoft的所有引用,并且一切正常

Microsoft.AspNetCore.Mvc.NewtonsoftJson
没有出现默认值。
尝试将此nuget软件包手动安装到您的服务项目中。这对我很有用。

我在将api从.netcore 2.2转换为.netcore 3时遇到了这个问题。 我的api返回转换为camelCase的响应,即使我的模型是PascalCase。 在
startup.cs
中:

.netcore 2:

services.AddMvc()
    .AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());
// keeps the casing of models when serializing to json (default is converting to camelCase)
services.AddMvc()
    .AddJsonOptions(options => options.JsonSerializerOptions.PropertyNamingPolicy = null); 
.netcore 3:

services.AddMvc()
    .AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());
// keeps the casing of models when serializing to json (default is converting to camelCase)
services.AddMvc()
    .AddJsonOptions(options => options.JsonSerializerOptions.PropertyNamingPolicy = null); 
这意味着您不需要导入newtonsoft.json。

可能的副本