如何关闭或处理 JSON 响应 ASP.NET Core 中的 camelCasing? [英] How to turn off or handle camelCasing in JSON response ASP.NET Core?
问题描述
我正在学习有关 ASP.NET Core/Web API/Angular 2 的 WintellectNOW 课程.我实现了 API 部分,但无论出于何种原因,返回的 JSON 的变量名称都被小写.>
返回的 JSON 格式如下...
<预><代码>[{"id":1,"name":"Bowler","color":"black","count":1},{"id":2,"name":"Fedora","color":"red","count":1},{"id":3,"name":"棒球帽","color":"blue","count":3}]我期待...
<预><代码>[{"Id":1,"Name":"Bowler","Color":"black","Count":1},{"Id":2,"Name":"Fedora","Color":"red","Count":1},{"Id":3,"Name":"Baseball Cap","Color":"blue","Count":3}]基于...的 C# 模型
命名空间 HatCollection.Models{公开课帽子{公共 int Id { 获取;放;}公共字符串名称{获取;放;}公共字符串颜色{获取;放;}公共 int 计数 { 得到;放;}}}
我什至用 [DataMember(Name = "Id")]
装饰属性只是为了确保它仍然无关紧要.
偶尔,它与控制器中的 Action 和实例变量相关...
私有静态只读列表<帽子>MyHats = 新列表<帽子>{新帽子 {Id = 1, Name = "Bowler", Color = "black", Count = 1 },新帽子 {Id = 2, Name = "Fedora", Color = "red", Count = 1 },新帽子 {Id = 3, Name = "Baseball Cap", Color = "blue", Count = 3 }};[HttpGet]公共 IEnumerable<帽子>得到(){返回 MyHats;}
如何关闭驼峰命名法功能,以便 ASP.NET Core 返回属性名称而不更改它们?
在 ASP.NET Core <3.0 中,JSON 属性默认为驼峰式(per 本公告).
您可以通过替换来禁用此功能
services.AddMvc();
与
服务.AddMvc().AddJsonOptions(opt => opt.SerializerSettings.ContractResolver= new DefaultContractResolver());
在您的 Startup.cs 文件中.您必须将 using Newtonsoft.Json.Serialization;
添加到文件顶部.
使用 DefaultContractResolver
后,属性名称将在 JSON 输出中逐字表示.不需要 DataMember
属性.
I'm running through a WintellectNOW course on ASP.NET Core/Web API/Angular 2. I have the API portion implemented, but for whatever reason, the JSON that is being returned has the variable names being lowercased.
The returned JSON is formatted like...
[
{"id":1,"name":"Bowler","color":"black","count":1},
{"id":2,"name":"Fedora","color":"red","count":1},
{"id":3,"name":"Baseball Cap","color":"blue","count":3}
]
I'm expecting...
[
{"Id":1,"Name":"Bowler","Color":"black","Count":1},
{"Id":2,"Name":"Fedora","Color":"red","Count":1},
{"Id":3,"Name":"Baseball Cap","Color":"blue","Count":3}
]
Based on the C# model of...
namespace HatCollection.Models
{
public class Hat
{
public int Id { get; set; }
public string Name { get; set; }
public string Color { get; set; }
public int Count { get; set; }
}
}
I even went as far as decorating the properties with [DataMember(Name = "Id")]
just to make sure and it still didn't matter.
On the off chance, it's relevant the Action and instance variable in the controller...
private static readonly List<Hat> MyHats = new List<Hat>
{
new Hat {Id = 1, Name = "Bowler", Color = "black", Count = 1 },
new Hat {Id = 2, Name = "Fedora", Color = "red", Count = 1 },
new Hat {Id = 3, Name = "Baseball Cap", Color = "blue", Count = 3 }
};
[HttpGet]
public IEnumerable<Hat> Get()
{
return MyHats;
}
How do I turn off the camelCase functionality, so that ASP.NET Core returns the property names without changing them?
In ASP.NET Core <3.0, JSON properties are camelCased by default (per this announcement).
You can disable this by replacing
services.AddMvc();
with
services
.AddMvc()
.AddJsonOptions(opt => opt.SerializerSettings.ContractResolver
= new DefaultContractResolver());
in your Startup.cs file. You'll have to add using Newtonsoft.Json.Serialization;
to the top of the file.
With the DefaultContractResolver
in place, the property names will be represented verbatim in the JSON output. No need for DataMember
attributes.
这篇关于如何关闭或处理 JSON 响应 ASP.NET Core 中的 camelCasing?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!