Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core 3.1 ignoring System.Text.Json.JsonPropertyName when returning response body

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

However, I have now noticed the application is ignoring the JsonPropertyNameAttribute and serializing the property name to camel case instead.

For example:

[JsonPropertyName("handsets")]
public IEnumerable<GetHandsetResponse> AllHandsets { get; set; }

The response object field is being output as allHandsets in the response and not handsets as specified by the JsonPropertyName.

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?

like image 452
bytedev Avatar asked Nov 25 '25 05:11

bytedev


1 Answers

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.

like image 152
bytedev Avatar answered Nov 28 '25 05:11

bytedev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!