⚙️ Asp.Net Core: Fix Model Binding Ignoring a Renamed Query Param
The default model binder matches query string parameters to model properties by property name, case-insensitively, with no automatic knowledge of any JsonPropertyName convention configured for the JSON body binder elsewhere in the same application, because those two binding paths use entirely separate conventions. Relying on an attribute that already correctly renames a field for JSON requests does nothing for query string binding, so a request that passes a parameter under its intended external name via the query string binds to null or default instead of the value actually sent.
🔎 The Problem
public class SearchRequest
{
[JsonPropertyName("q")] // renames JSON body field "q" -> Query
public string Query { get; set; }
}
[HttpGet("search")]
public IActionResult Search([FromQuery] SearchRequest req) => Ok(req.Query);
// GET /search?q=widgets
// req.Query is null - [JsonPropertyName] only affects JSON
// body (de)serialization, never query string model binding,
// which still looks for a parameter literally named "Query"
✅ Fix: Rename for Each Binding Source Separately
- Add
[FromQuery(Name = "q")]directly on the property, alongside rather than instead of[JsonPropertyName]if the same model is also bound from a JSON body elsewhere, since each binding source needs its own rename attribute. - Write one integration test per bound model that actually sends a request over the query string with the renamed parameter, rather than only testing the JSON body path, since the two binding sources can silently diverge exactly like this with no compiler warning.
- Where the same model genuinely needs to bind from both a query string and a JSON body under different attributes, document both directly above the property so the next person editing it can’t miss that one attribute isn’t enough.
🚨 Why This Is Dangerous
- A search or filter parameter that silently binds to its default value instead of throwing looks exactly like no results matched, and a user’s search query being ignored is indistinguishable from a search that genuinely found nothing until someone thinks to check the raw query string.
Renaming a field for JSON doesn’t rename it for a query string too — ASP.NET Core’s two binding paths each need to be told separately.





