Convention-Based Routing Basics
Convention-based routing maps URL patterns to controllers and actions using a route template registered centrally, most commonly the default template {controller=Home}/{action=Index}/{id?} registered via app.MapControllerRoute(name: "default", pattern: "{controller=Home}/{action=Index}/{id?}") in Program.cs (or RouteConfig.cs in classic ASP.NET MVC). Each segment in curly braces is a route parameter that the routing system extracts from the URL and later uses to select a controller, select an action, and bind method parameters; segments can specify default values with an equals sign, as {controller=Home} does, and a trailing question mark like {id?} marks a segment optional, meaning /Products/Details will still match even though the id token is absent.
Cricket analogy: It is like a scorecard template with fixed slots for batter, runs, and balls faced, where an unfinished innings simply leaves the 'not out' slot blank rather than breaking the whole scorecard format.
Route Matching and Parameter Binding
When a request arrives, the routing middleware compares the URL against every registered route in order, and the first template whose segments and constraints all match wins; unmatched literal segments (route templates can mix literals and parameters, like "blog/{year}/{month}/{slug}") must match exactly, while parameter segments capture whatever text occupies that position, subject to any inline constraints such as {id:int} which requires the segment to parse as an integer or the route simply does not match. Once a route matches, its extracted route values (controller, action, and any additional tokens like id) feed directly into model binding, so a template of {controller}/{action}/{id?} against the URL /Products/Details/42 produces controller=Products, action=Details, and an id parameter of 42 that model binding then supplies to the action method's int id parameter.
Cricket analogy: It is like an umpire checking a bowler's field placement against the exact fielding restriction rule for that phase of a T20 innings; if even one fielder is out of position, the delivery is called a no-ball rather than being partially accepted.
// Program.cs (ASP.NET Core)
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllersWithViews();
var app = builder.Build();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
name: "blog",
pattern: "blog/{year:int}/{month:int}/{slug}",
defaults: new { controller = "Blog", action = "Post" });
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();Route order matters for convention-based routing: more specific templates like the 'blog' route above must be registered before the general-purpose 'default' route, otherwise the default route's {controller}/{action}/{id?} pattern would greedily match /blog/2026/07/my-post first and route it incorrectly.
Generating URLs from Route Values
Routing works in both directions: besides matching an incoming URL, it can also generate an outbound URL from a set of route values, which is what Url.Action("Details", "Products", new { id = 42 }) and the Razor tag helper asp-controller/asp-action rely on internally. The link generator walks the same registered route table looking for a template that can produce a URL matching the supplied controller, action, and extra values, substituting them into the template's parameter slots and omitting segments whose value equals the route's registered default, which is why linking to the Home controller's Index action typically produces just "/" rather than "/Home/Index".
Cricket analogy: It is like a fixture-list generator that, given a team name and round number, reconstructs the exact ground and date for that match by consulting the same schedule template used to publish the original fixture list.
If you rename a controller or action without updating all Url.Action, Html.ActionLink, or asp-action references, link generation will silently produce a broken URL rather than a compile-time error, since these values are plain strings resolved at runtime.
- Convention-based routing maps URL templates like {controller}/{action}/{id?} to controllers and actions.
- Route segments can declare default values ({controller=Home}) and optional segments ({id?}).
- Inline constraints such as {id:int} require a segment to match a specific type or format to be considered a match.
- Routes are evaluated in registration order, so more specific routes must be registered before general ones.
- Matched route values feed directly into model binding for the selected action's parameters.
- Routing also works in reverse to generate URLs via Url.Action and the asp-action tag helper.
- Renaming a controller or action without updating all link-generation call sites produces broken links only discoverable at runtime.
Practice what you learned
1. In the default route template {controller=Home}/{action=Index}/{id?}, what does the trailing '?' on {id?} indicate?
2. Why must a more specific route like 'blog/{year:int}/{month:int}/{slug}' be registered before the general default route?
3. What does an inline constraint like {id:int} do?
4. What is the purpose of Url.Action("Details", "Products", new { id = 42 })?
5. What happens if a route value supplied to Url.Action equals a route's registered default value?
Was this page helpful?
You May Also Like
Attribute Routing
Learn how to declare routes directly on controllers and actions with attributes, and how this approach complements or replaces convention-based routing.
Controllers Basics
Learn what a controller is in ASP.NET MVC, how it is discovered and instantiated, and how it coordinates models and views to handle a request.
Action Methods and Action Results
Understand how public controller methods become callable actions and how the IActionResult family shapes the HTTP response returned to the client.
Related Reading
Related Study Notes in Microsoft Technologies
Browse all study notesWindows 10 / UWP Development Study Notes
.NET · 30 topics
Microsoft TechnologiesWindows Batch Scripting Study Notes
Batch · 30 topics
Microsoft TechnologiesMFC (Microsoft Foundation Classes) Study Notes
C++ · 30 topics
Microsoft TechnologiesSilverlight Study Notes
.NET · 30 topics
Microsoft TechnologiesXAML Study Notes
.NET · 30 topics
Microsoft TechnologiesWPF (Windows Presentation Foundation) Study Notes
.NET · 30 topics