Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio

Bits of .NET

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

  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio
Asp.Net Core

Asp.Net Core: Fix Model Binding Dropping a Record’s Init Property

- 26.11.26 - ErcanOPAK

⚙️ Asp.Net Core: Fix Model Binding Dropping a Record’s Init Property

ASP.NET Core’s default model binder constructs a record by calling its primary constructor and then separately assigning any settable property that wasn’t part of that constructor – but an init-only property can only ever be assigned during object initialization, not through a regular property setter called afterward. For a record whose primary constructor doesn’t happen to include every init property (a property added later directly on the record body, for instance, rather than through the constructor), the binder silently leaves it at its default value instead of throwing, because reflection-based binding that can’t find a settable path simply skips the property rather than failing the whole bind.

🔎 The Problem

public record CreateOrderRequest(string CustomerId, decimal Total)
{
    public string? PromoCode { get; init; }  // not in the constructor
}

// POST body: { "customerId": "c-42", "total": 199.99, "promoCode": "SAVE10" }
// Bound request.PromoCode == null - silently dropped, no binding error,
// no validation failure, request proceeds as if the field was never sent

✅ The Fix

  • Include every property the client is expected to send directly in the record’s primary constructor parameter list, so System.Text.Json and the model binder both populate it through the constructor the way init-only properties are actually designed to be set.
  • Add an explicit integration test that posts a full JSON payload and asserts every property on the bound object, rather than only testing the properties that happen to be in the constructor – a silently-dropped property passes a test that never checked it.
  • If you must add a property to the record body after the constructor is already locked in for other reasons, bind the raw request to a plain DTO first and map it into the record explicitly, rather than relying on the framework’s default binder to find a path to an init-only property it can’t reach.

🚨 Why This Is Dangerous

A silently dropped field produces a 200 OK with no validation error and no indication anything was wrong – the request LOOKS like it succeeded completely, while the specific value the client sent (a promo code, a discount flag, a compliance acknowledgment) is quietly discarded before it ever reaches your business logic.

An init-only property is only as reachable as the constructor that’s allowed to set it – leave it out of the constructor, and the binder has nowhere left to put your data.

— Backend Architect

Related posts:

.NET Core: Use IMemoryCache for In-Memory Caching Without Redis

.NET Core: Use IMemoryCache for In-Memory Caching Without Redis

.NET Core: Native Rate Limiting - Shielding Your API from Abuse

.NET Core: Native Rate Limiting - Shielding Your API from Abuse

.NET Core Logging Hurts Performance

.NET Core Logging Hurts Performance

Views: 8
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

Kubernetes: Fix a NetworkPolicy Silently Blocking DNS Egress
Asp.Net Core: Fix Health Checks Reporting Healthy During an Outage

Leave a Reply Cancel reply

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

Most Read

  1. 01How to make theater mode the default for Youtube 1,080 views
  2. 02How to add default value for Entity Framework migrations for DateTime and Bool 999 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 998 views
  4. 04Get the First and Last Word from a String or Sentence in SQL 874 views
  5. 05How to select distinct rows in a datatable in C# 869 views
  6. 06How to enable, disable and check if Service Broker is enabled on a database in SQL Server 673 views
  7. 07Add Constraint to SQL Table to ensure email contains @ 632 views
  8. 08Average of all values in a column that are not zero in SQL 586 views
  9. 09How to use Map Mode for Vertical Scroll Mode in Visual Studio 563 views
Bits of .NET

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

Social

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