In ASP.NET MVC, routing is the mechanism that maps incoming URLs to
controller actions. It’s essential to delivering requests to the right code based on URL patterns.
1. Routing Table (Convention-Based Routing)
Defined in RouteConfig.cs (inside App_Start).
The application uses URL patterns to route requests.
Example:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
URL:/Products/Details/5 → Maps to:ProductsController.Details(int id = 5)
This is convention-based routing — the route depends on the folder/controller/action structure.
2. Route Matching Process
When a request comes in:
MVC reads the routing table in order.
It matches the request URL to the defined route pattern.
It determines:
Which controller to instantiate.
Which action method to invoke.
What parameters to pass.
What is Attribute Routing?
Attribute Routing lets you define routes directly on controller actions using
[Route] attributes instead of relying on the centralized route table.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
In ASP.NET MVC, routing is the mechanism that maps incoming URLs to controller actions. It’s essential to delivering requests to the right code based on URL patterns.
1. Routing Table (Convention-Based Routing)
Defined in
RouteConfig.cs(insideApp_Start).The application uses URL patterns to route requests.
Example:
URL:
/Products/Details/5→Maps to:
ProductsController.Details(int id = 5)2. Route Matching Process
When a request comes in:
What is Attribute Routing?
Attribute Routing lets you define routes directly on controller actions using
[Route]attributes instead of relying on the centralized route table.Enabling Attribute Routing
In
RouteConfig.cs:Example: Attribute Routing
URL:
/articles/view/10→ hitsViewArticle(10)URL:
/articles/delete/10→ hitsDeleteArticle(10)Attribute Routing with Constraints
Adds validation to ensure only integers for
yearandmonth.Benefits of Attribute Routing
Summary
{controller}/{action}/{id})[Route]Routing = URL → Controller + Action + Parameters