Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
Asp.Net Core

ASP.NET Core: Fix a FromBody Parameter Deserializing to All-Default Values Because Two Competing JSON Input Formatters Are Registered

- 29.10.26 - ErcanOPAK

⚙️ Fix a FromBody Parameter Deserializing to All-Default Values Because Two Competing JSON Input Formatters Are Registered

Registering both AddNewtonsoftJson() and the default System.Text.Json-based formatters – common during a migration, or when some NuGet package calls AddNewtonsoftJson internally without anyone noticing – puts two input formatters in the pipeline that can disagree on casing and attribute conventions. Whichever one runs first for a given application/json request wins, and if that’s the formatter that doesn’t understand your model’s actual attributes, the body still “deserializes” successfully into an object with every property left at its default value, with no error raised anywhere.

🔎 The Problem

// Startup:
services.AddControllers().AddNewtonsoftJson();

// Model uses a System.Text.Json attribute:
public class CreateUserRequest
{
    [JsonPropertyName("user_name")]
    public string UserName { get; set; }
}

// Action receives a non-null CreateUserRequest object,
// but UserName is null - Newtonsoft's formatter ran
// first and has no idea what [JsonPropertyName] means.

✅ Fix: Standardize on One JSON Stack for the Whole Pipeline

  • Pick exactly one JSON stack for the entire application – Newtonsoft or System.Text.Json – and remove the other’s registration entirely, instead of leaving both present just because some dependency happened to need one of them once.
  • If both genuinely must coexist for different reasons (a legacy area needing Newtonsoft’s specific feature set), scope that need with per-controller or per-area formatter configuration instead of registering it globally for the whole app.
  • Add a startup-time check – even a simple log line enumerating MvcOptions.InputFormatters – so a future package upgrade that silently re-adds a second JSON formatter gets caught in code review instead of discovered through a support ticket.

🚨 Why This Is Dangerous

  • This fails completely silently – no exception, no 400 response, just a successfully bound object full of default values – so never trust “the request bound without error” as proof the body was actually read correctly; validate that meaningfully required fields are non-default before acting on the model.

A model with every property at its default value isn’t empty by accident – something successfully deserialized nothing into it.

— Backend Architect

Related posts:

.NET Core: Use ActionFilters for Cross-Cutting Concerns

.NET Core: Use ActionFilters for Cross-Cutting Concerns

.NET Core: BenchmarkDotNet ile Performance Testing

.NET Core: BenchmarkDotNet ile Performance Testing

Why Minimal APIs Improve Cold Start

Why Minimal APIs Improve Cold Start

Views: 8

Post navigation

Git: Fix a Commit’s Author Date and Commit Date Showing Different Years Because an Old Patch Was Applied With git am Long After It Was Written
Photoshop: Fix the Type Tool’s Font Preview Showing the Wrong Typeface Because a Separate Font Manager App Is Overriding Which Font File Actually Loads

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

November 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (981)
  • How to add default value for Entity Framework migrations for DateTime and Bool (977)
  • Get the First and Last Word from a String or Sentence in SQL (858)
  • How to select distinct rows in a datatable in C# (854)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (653)
  • Add Constraint to SQL Table to ensure email contains @ (609)
  • Average of all values in a column that are not zero in SQL (570)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (547)
  • Find numbers with more than two decimal places in SQL (475)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit
  • SQL Server: Fix ROW_NUMBER Ties Reordering Between Identical Runs
  • C#: Fix an Enum HasFlag Returning True for the Zero Value
  • WordPress: Fix Featured Images Vanishing After a CDN Migration
  • Photoshop: Fix Content-Aware Fill Smearing a Repeating Pattern
  • Asp.Net Core: Fix Antiforgery Tokens Rejected Behind a Reverse Proxy

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (981)
  • How to add default value for Entity Framework migrations for DateTime and Bool (977)
  • Get the First and Last Word from a String or Sentence in SQL (858)
  • How to select distinct rows in a datatable in C# (854)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com