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 Ignoring a Renamed Query Param

- 24.11.26 - ErcanOPAK

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

— Backend Architect

Related posts:

.NET Core: Use Data Annotations for Model Validation

.NET Core: Use Data Annotations for Model Validation

.NET Core: Use BenchmarkDotNet to Accurately Measure Performance

.NET Core: Use BenchmarkDotNet to Accurately Measure Performance

Security — URL Length Can Kill Requests

Security — URL Length Can Kill Requests

Views: 11
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

SQL Server: Fix a CDC Job Falling Behind After Log Truncate
The AI Prompt That Turns a Moving Quote Into a Red-Flag Checklist

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 997 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 996 views
  4. 04Get the First and Last Word from a String or Sentence in SQL 873 views
  5. 05How to select distinct rows in a datatable in C# 868 views
  6. 06How to enable, disable and check if Service Broker is enabled on a database in SQL Server 672 views
  7. 07Add Constraint to SQL Table to ensure email contains @ 631 views
  8. 08Average of all values in a column that are not zero in SQL 585 views
  9. 09How to use Map Mode for Vertical Scroll Mode in Visual Studio 562 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