⚙️ 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.






