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 Minimal API Route Matching the Wrong Overload

- 17.11.26 - ErcanOPAK

⚙️ Asp.Net Core: Fix a Minimal API Route Matching the Wrong Overload

Minimal API routing generally favors a literal path segment over a parameterized one, but that preference only kicks in reliably when the parameterized segment carries an explicit route constraint — a bare {id} with no type constraint can, depending on registration order and how routes are split across MapGroup calls, end up treated as equally specific to a literal sibling segment, so /orders/latest can be silently routed into the handler meant for /orders/{id}, receiving the literal string "latest" as the id instead of ever reaching its own handler.

🔎 The Problem

app.MapGet("/orders/{id}", (string id) =>
{
    // receives id = "latest" instead of "/orders/latest" ever
    // reaching the handler below, because {id} has no constraint
    return LookupOrderById(id);
});

app.MapGet("/orders/latest", () => GetMostRecentOrder());
// registered AFTER the parameterized route above - and without a
// constraint on {id}, routing does not reliably prefer this literal
// segment over the earlier parameterized one in every scenario

✅ Fix: Constrain the Parameter, Don’t Rely on Registration Order

  • Add an explicit route constraint to the parameterized segment ({id:int}, {id:guid}) whenever a literal sibling route could otherwise collide with it — a constrained parameter route simply can’t match a literal string like "latest" in the first place.
  • Register more specific literal routes before parameterized ones explicitly, as a second line of defense, rather than assuming ASP.NET Core’s specificity rules alone will always resolve the collision the way you expect across every grouping and ordering combination.
  • Add an integration test that specifically hits the literal route (/orders/latest) and asserts which handler responded — a routing collision like this compiles perfectly cleanly and only shows up as a wrong response body at runtime.

🚨 Why This Is Dangerous

  • Returning a 200 OK with order id “latest” silently resolved by the parameterized handler’s own database lookup logic — which might legitimately return null, or worse, a coincidentally matching record — instead of a clear 404 or the actually intended ‘most recent order’ response.

An unconstrained route parameter doesn’t just accept anything — it can quietly outcompete the literal route you meant to hit.

— Backend Architect

Related posts:

ASP.NET Core Apps Consume More Memory Over Time

ASP.NET Core Apps Consume More Memory Over Time

ASP.NET Core “Response Compression Not Working” — The Missing MIME Types

ASP.NET Core “Response Compression Not Working” — The Missing MIME Types

ASP.NET Core: Fix an Authorize Policy That Silently Never Runs Because It Was Applied to a Minimal A...

ASP.NET Core: Fix an Authorize Policy That Silently Never Runs Because It Was Applied to a Minimal A...

Views: 5

Post navigation

Windows 11: Fix Bluetooth Audio Stuttering Right After Waking Up
WordPress: Fix the Media Library Showing Dead Thumbs After an S3 Move

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,059)
  • Get the User Name and Domain Name from an Email Address in SQL (983)
  • How to add default value for Entity Framework migrations for DateTime and Bool (981)
  • Get the First and Last Word from a String or Sentence in SQL (861)
  • How to select distinct rows in a datatable in C# (856)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (658)
  • Add Constraint to SQL Table to ensure email contains @ (613)
  • Average of all values in a column that are not zero in SQL (571)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (549)
  • Find numbers with more than two decimal places in SQL (477)

Recent Posts

  • Visual Studio: Fix Quick Actions Hiding Behind a Stale Squiggle
  • Photoshop: Fix a Smart Filter Resetting After You Relink It
  • Ajax: Fix a Cancelled Request Still Updating Stale UI State
  • C#: Fix a Span Captured by a Lambda Throwing at Runtime
  • The AI Prompt That Converts a REST Endpoint Into a Typed SDK
  • Git: Fix a Worktree Locking a Branch Needed Somewhere Else
  • Asp.Net Core: Fix a Background Service Leaking Scoped Dependencies
  • SQL Server: Fix a Columnstore Index Fragmenting After Bulk Deletes
  • Docker: Fix a Container Losing Logs After a Restart Policy
  • Asp.Net Core: Fix Output Caching Serving a Stale Authorized Page

Most Viewed Posts

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

Recent Posts

  • Visual Studio: Fix Quick Actions Hiding Behind a Stale Squiggle
  • Photoshop: Fix a Smart Filter Resetting After You Relink It
  • Ajax: Fix a Cancelled Request Still Updating Stale UI State
  • C#: Fix a Span Captured by a Lambda Throwing at Runtime
  • The AI Prompt That Converts a REST Endpoint Into a Typed SDK

Social

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