100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
.NET Framework

Routing in MVC

See how convention-based routing maps incoming URLs to controller actions using route templates, defaults, and constraints.

Controllers and ActionsBeginner9 min readJul 10, 2026
Analogies

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.

csharp
// 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

Was this page helpful?

Topics covered

#NETFramework#ASPNETMVCStudyNotes#MicrosoftTechnologies#RoutingInMVC#Routing#MVC#Convention#Based#StudyNotes#SkillVeris