How to set json serializer settings in ASP.NET Core
By FoxLearn 3/4/2025 2:56:40 AM 378
In ASP.NET Core, the AddMvc
method returns an IMvcBuilder
implementation, which provides an extension method called AddJsonOptions
to configure JSON serialization settings. The newer methods AddControllers
, AddControllersWithViews
, and AddRazorPages
also return an IMvcBuilder
and can be chained with AddJsonOptions
in the same way as AddMvc
. This allows you to configure JSON serialization settings for your controllers, Razor Pages, or MVC views.
To configure the JSON serialization globally, you need to modify the AddControllers
or AddMvc
method in the ConfigureServices
method of your Startup.cs
or Program.cs
file (for ASP.NET Core 6 and later).
using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Newtonsoft.Json; // If you want to use Newtonsoft.Json var builder = WebApplication.CreateBuilder(args); // Configure JSON serialization settings builder.Services.AddControllers() .AddJsonOptions(options => { // Example: Use camel case for property names options.JsonSerializerOptions.PropertyNamingPolicy = System.Text.Json.JsonNamingPolicy.CamelCase; // Example: Ignore null values options.JsonSerializerOptions.DefaultIgnoreCondition = System.Text.Json.JsonIgnoreCondition.WhenWritingNull; // Example: Configure custom converters if needed // options.JsonSerializerOptions.Converters.Add(new YourCustomConverter()); }); // If you want to use Newtonsoft.Json instead of System.Text.Json builder.Services.AddControllers() .AddNewtonsoftJson(options => { // Example: Camel case property names using Newtonsoft.Json options.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver(); // Example: Ignore null values using Newtonsoft.Json options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore; }); var app = builder.Build(); // Use the application app.MapControllers(); app.Run();
For versions prior to ASP.NET Core 6, you would typically use Startup.cs
. Here's how to configure JSON serialization settings in ConfigureServices
.
public void ConfigureServices(IServiceCollection services) { services.AddControllers() .AddJsonOptions(options => { // Example: Use camel case for property names options.JsonSerializerOptions.PropertyNamingPolicy = System.Text.Json.JsonNamingPolicy.CamelCase; // Example: Ignore null values options.JsonSerializerOptions.DefaultIgnoreCondition = System.Text.Json.JsonIgnoreCondition.WhenWritingNull; // Example: Add custom converters // options.JsonSerializerOptions.Converters.Add(new YourCustomConverter()); }); // If you want to use Newtonsoft.Json instead of System.Text.Json services.AddControllers() .AddNewtonsoftJson(options => { // newtonsoft json serializer settings // Example: Camel case property names options.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver(); // Example: Ignore null values options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore; }); }
You can control the behavior of JSON serialization on a per-action basis or globally via middleware.
[HttpGet] public IActionResult Get() { var result = new { Name = "John", Age = 30 }; return new JsonResult(result, new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull }); }
To set JSON serializer settings globally in ASP.NET Core, you generally modify the AddJsonOptions
method in your Program.cs
or Startup.cs
file. You can use System.Text.Json
or Newtonsoft.Json
based on your preference.
- How to securely reverse-proxy ASP.NET Core
- How to Retrieve Client IP in ASP.NET Core Behind a Reverse Proxy
- Only one parameter per action may be bound from body in ASP.NET Core
- The request matched multiple endpoints in ASP.NET Core
- How to Create a custom model validation attribute in ASP.NET Core
- How to disable ModelStateInvalidFilter in ASP.NET Core
- How to fix LoginPath not working in ASP.NET Core
- Synchronous operations are disallowed