Minimal API: JSON defaulting to cascalCase

10/25/2024

A few years back I had this same issue while developing my Quid app. This time I'm writing a home security system using a minimal API. And again, I'm BLOG-ing this more for my own sake.

Minimal REST APIs default to camelCase JSON

So apparently the default behavior of the JSON serializer for .NET Core minimal APIs is to convert all property names to camelCase rather than leaving them PascalCase. The fix was short, but not necessarily simple to find.

TL;DR;

Ok, here's the fix, for those searching for a quick answer. Pay attention to the namespace! or read below.

builder.Services.Configure<Microsoft.AspNetCore.Http.Json.JsonOptions>(options =>
{
    options.SerializerOptions.PropertyNamingPolicy = null;
});

Obviously place this line of code where your builder setup code is located

Heed the Namespace issue

When I first ran this using other code I found, it didn't work. Putting a breakpoint inside that anonymous function showed it wasn't being called. The problem was I was using the wrong JsonOptions based on the namespace usings in my file. It was using:

Microsoft.AspNetCore.Mvc.Json

Instead of:

Microsoft.AspNetCore.Http.Json

A chain is only as strong as its weakest link

So anyway, there's the JSON serialization fix...for future reference (Jeff).


Please register or login to add a comment.

Comments (displaying 1 - 1):
No comments yet! Be the first...


  • C#/.NET
  • T-SQL
  • HTML/CSS
  • JavaScript/jQuery
  • .NET 8
  • ASP.NET/MVC
  • Xamarin/MAUI
  • WPF
  • Windows 11
  • SQL Server 20xx
  • Android
  • XBox
  • Arduino
  • Skiing
  • Rock Climbing
  • White water kayaking
  • Road Biking