返回响应正文时,ASP.NET Core 3.1忽略System.Text.Json.JsonPropertyName [英] ASP.NET Core 3.1 ignoring System.Text.Json.JsonPropertyName when returning response body
问题描述
我最近将ASP.NET Core应用程序从2.2更改为目标3.1.我也从使用Newtonsoft进行JSON序列化更改为System.Text.Json.在此过程中,我已更改了许多响应类属性以使用JsonPropertyNameAttribute(来自Newtonsoft JsonPropertyAttribute).
I recently changed my ASP.NET Core application to target 3.1 from 2.2. I also changed from using Newtonsoft for JSON serialization to System.Text.Json. In the process I have changed a number of response class properties to use JsonPropertyNameAttribute (from the Newtonsoft JsonPropertyAttribute).
但是,我现在注意到应用程序忽略了JsonPropertyNameAttribute,而是将属性名称序列化为驼峰式大小写.
However, I have now noticed the application is ignoring the JsonPropertyNameAttribute and serializing the property name to camel case instead.
例如:
[JsonPropertyName("handsets")]
public IEnumerable<GetHandsetResponse> AllHandsets { get; set; }
响应对象字段在响应中以 allHandsets
输出,而不是JsonPropertyName指定的 handsets
.
The response object field is being output as allHandsets
in the response and not handsets
as specified by the JsonPropertyName.
我猜我在应用程序的Startup.cs中缺少某些内容来告诉它使用System.Text.Json,但是我不知道该怎么办.我可能会缺少什么?
I'm guessing I'm missing something in the Startup.cs of the application to tell it to use System.Text.Json however I have no idea what. What might I be missing?
推荐答案
看来,在从Startup.cs-应用程序的ConfigureServices()调用的通用代码的最深处,有一个对 .AddNewtonsoftJson()的调用.
(在返回的IMvcBuilder类型上).删除该方法调用似乎可以正确地将应用程序还原为使用System.Text.Json序列化器.
It appears that deep down in the common code called from Startup.cs - ConfigureServices() of the application there is a call to .AddNewtonsoftJson()
(on the returned IMvcBuilder type). Removing that method call appears to properly revert the application back to using the System.Text.Json serializer.
这篇关于返回响应正文时,ASP.NET Core 3.1忽略System.Text.Json.JsonPropertyName的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!