In this article I will explain with an example, how to use System.Text.Json JsonSerializer to serialize JSON in PascalCase in ASP.Net Core (.Net Core 6).
Note: For beginners in ASP.Net Core (.Net Core 6), please refer my article ASP.Net Core 6: Hello World Tutorial with Sample Program example.
 
 

Need to change the JSON Serializer setting

The ASP.Net Core (.Net Core 6) 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.
.Net Core 6: System.Text.Json JsonSerializer serialize PascalCase
 
 

Configuring JSON Serializer setting

The first step is to configure the JSON Serializer settings in the Program.cs file.
1. Open the Program.cs class from the Solution Explorer window.
.Net Core 6: System.Text.Json JsonSerializer serialize PascalCase
 
2. Inherit the following namespace.
using Newtonsoft.Json.Serialization;
 
3. Inside Program.cs class, the AddJsonOptions method of the Services property is called where the PropertyNamingPolicy is set to NULL.
This will remove the default Camel Case naming policy and JSON property names will remain intact as defined in the classes.
var builder = WebApplication.CreateBuilder(args);
 
// Enabling MVC
builder.Services.AddControllersWithViews()
    .AddJsonOptions(options => options.JsonSerializerOptions.PropertyNamingPolicy = null);
 
Note: For working full sample, please refer my article .Net Core 6: Using jQuery AJAX in ASP.Net Core.