I'm trying to change all the property names to snake_case globally in my ASP.NET Core 3.0 API, but I couldn't find a way.
Previously, in ASP.NET Core 2.2, I used:
services.AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver() { NamingStrategy = new SnakeCaseNamingStrategy() })
Now in ASP Net Core 3.0 the only thing that I found barely similar was:
services.AddJsonOptions(options => options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase)
But snake_case is not present.
Is there a such way to make snake_case for the request and response objects globally in my ASP.NET Core 3.0 API?
I found the solution after read https://learn.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-2.2&tabs=visual-studio
The new code is:
.AddNewtonsoftJson(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver() { NamingStrategy = new SnakeCaseNamingStrategy() })
For .NET Core 3.0, use:
.AddJsonOptions(o => o.JsonSerializerOptions.PropertyNamingPolicy = null)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With