In this article I will explain with an example, how to change the default Camel Case JSON Output in ASP.Net Core (.Net Core 2).
Note: For beginners in ASP.Net Core (.Net Core 2), please refer my article ASP.Net MVC Core Hello World Tutorial with Sample Program example.
 
 

Need to change the JSON Serializer setting

The ASP.Net Core (.Net Core 2) AJAX program will work, but you will see undefined values in the JavaScript Alert Message Box (as shown below) when you try to parse the JSON.
ASP.Net Core 2: Changing the default Camel Case JSON Output
 
 

Configuring JSON Serializer setting

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.
ASP.Net Core 2: Changing the default Camel Case JSON Output
 
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 ContractResolver is set to DefaultContractResolver object which will instruct the program to use Newtonsoft library for JSON serialization.
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.AddMvc()
            .AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());
}
 
Note: For working full sample, please refer my article .Net Core 2: Using jQuery AJAX in ASP.Net Core.


Other available versions