从 ASP.Net Core 1.0.0-rc2-final 到1.0.0的交换中,JSON 属性现在是小写

我刚把我们的项目和 ASP 交换了。Net Core 1.0.0-rc2-final to 1.0.0.我们的网站和客户端已经停止工作,因为大写的 JSON 属性。例如,这一行 JavaScript 现在失败了

for (var i = 0; i < collection.Items.length; i++){

因为控制器现在调用数组“ item”而不是“ Items”。除了安装更新的包和编辑 project.json 文件之外,我没有做任何更改。我没有更改仍然大写其属性的 C # 模型文件。

Why have the ASP.Net Core controllers started returning JSON with lower-cased properties? How do I go back to them honoring the case of the property names from the model?

66618 次浏览

你可以像这样改变行为:

services
.AddMvc()
.AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());

请看这里的公告: https://github.com/aspnet/Announcements/issues/194

MVC 现在默认使用驼峰大小写名称序列化 JSON

默认情况下,使用此代码可避免使用驼峰大小写名称

  services.AddMvc()
.AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());

来源: https://github.com/aspnet/Announcements/issues/194

对于那些使用 ASP.net WEB API (而不是 ASP.NET 核心)的人来说。

在 WebApiConfig 中添加此行。

//Comment this jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();


jsonFormatter.SerializerSettings.ContractResolver = new DefaultContractResolver();

在这里添加这个作为一个答案,因为这在谷歌搜索网页应用程序应用程序接口中也是首先出现的。

万一你在 Google 上找到了这个并且正在寻找一个针对 Core 3的解决方案。

Core 3 uses System.Text.Json, which by default does not preserve the case. As mentioned with this GitHub 的问题, setting the PropertyNamingPolicy to null will fix the problem.

public void ConfigureServices(IServiceCollection services)
{
...
services.AddControllers()
.AddJsonOptions(opts => opts.JsonSerializerOptions.PropertyNamingPolicy = null);

如果你不想改变全局设置,只有一个操作是这样的:

return Json(obj, new JsonSerializerOptions { PropertyNamingPolicy = null });

这将修复它在 dotnet 核心3 webapi,所以它不会改变你的属性名称,你返回到你的客户端正是你想要的。

在 Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
services.AddControllers().AddJsonOptions(options => options.JsonSerializerOptions.PropertyNamingPolicy = null);
services.AddHttpClient();
}

对于 Core 2.x 版本,使用此代码可以在默认情况下避免使用驼峰大小写名称。您需要在 Startup.cs 文件的 ConfigureServices 方法中添加以下代码。

services.AddMvc()
.AddJsonOptions(o =>
{
if (o.SerializerSettings.ContractResolver != null)
{
var castedResolver = o.SerializerSettings.ContractResolver
as DefaultContractResolver;


castedResolver.NamingStrategy = null;
}
});

For those who migrated to Core 3.1 and have Core MVC project can use following setup code in Startup.cs:


        public void ConfigureServices(IServiceCollection services)
{
...
services.AddControllersWithViews().AddJsonOptions(opts => opts.JsonSerializerOptions.PropertyNamingPolicy = null);
...
}

For someone who does not want to set it globally, it is possible to use ContractResolver also to return as Json result:

public IActionResult MyMethod()
{
var obj = new {myValue = 1};
return Json(obj, new JsonSerializerSettings {ContractResolver = new DefaultContractResolver()});
}

最近在.Net6上出现了这个问题

解决方案是我需要安装的

牛顿软件 Json 6.0.0.x (注意,对于.Net 7使用7.x)

我在梅森的邮件里发现了这个:

牛顿软件 Json 6.0.2与 net5.0不兼容