How does routing work in ASP.NET MVC? Explain attribute routing.
Ask by ICSM Computer
Updated 12 Jun 2025
How Routing Works in ASP.NET MVC
Routing in ASP.NET MVC maps incoming URL requests to controller actions. It decouples the URL structure from the physical file paths, allowing clean, user-friendly URLs.
1. Conventional Routing
Defined in
RouteConfig.cs(usually inApp_Startfolder):Example URL Matching:
/Home/Index/Product/Details/52. Attribute Routing
Instead of defining routes globally, you place route attributes directly on controllers and actions.
Enable it (usually in
RouteConfig.cs):Example: Using Attribute Routing
This maps:
/products/list→ProductsController.List()/products/details/10→ProductsController.Details(10)Advanced Attribute Routing
Optional Parameters
Matches
/blog/2025and/blog/2025/06Route Names and Constraints
idis an integer ≥ 1Url.RouteUrl("OrderDetails", new { id = 5 })to generate the URLSummary: Conventional vs Attribute Routing
When to Use Attribute Routing