In this article I will explain with an example, how to use System.Text.Json
JsonNamingpolicy to set
CamelCase for
JSON in
ASP.Net Core (.Net Core 3).
Configuring JSON Serializer setting to enable Camel Case
The first step is to configure the
JSON Serializer settings in the
Startup.cs file.
1. Open the Startup.cs class from the Solution Explorer window.
2. Inherit the following namespace.
using Newtonsoft.Json.Serialization;
3. Inside the ConfigureServices method of Startup.cs class, the AddJsonOptions method of the services object is called where the PropertyNamingPolicy is set JsonNamingPolicy.
This will remove the default
Camel Case naming policy and
JSON property names will remain intact as defined in the classes.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews().SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
});
}