ASP.NET MVCrouting is a pattern matching system that is responsible for mapping incoming browser requests to specified MVC controller actions. When a user requests a URL, the routing engine matches the URL against the registered routes and executes the controller action that is associated with the matching route.
Routing is configured in the RouteConfig class, which is located in the App_Start folder of the application. The RouteConfig class contains a collection of Route objects, which define the URL patterns and controller actions that are associated with each route.
To create a new route, you need to specify the following information:
The URL pattern for the route.
The controller name that is associated with the route.
The action name that is associated with the route.
Any parameters that are associated with the route.
For example, the following code defines a route that maps the URL /products/view/ to the ProductsController class and the View action:
Code snippet
routes.MapRoute(
name: "Products",
url: "products/view/{id}",
defaults: new { controller = "Products", action = "View", id = UrlParameter.Optional }
);
When a user requests the URL /products/view/1, the routing engine will match the URL against the Products route and execute the ProductsController.View action. The ProductsController.View action will then render the view for the product with the ID of 1.
ASP.NET MVC also supports custom routing constraints, which can be used to restrict the values that are allowed for route parameters. For example, the following code defines a custom routing constraint that ensures that the value of the id parameter is a positive integer:
public class PositiveIntegerConstraint :
IRouteConstraint {
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{ int id; if (!int.TryParse(values[parameterName].AttemptedValue, out id) || id <= 0)
{ return false; }
return true;
}
}
The PositiveIntegerConstraint can then be used to restrict the values that are allowed for the id parameter in the Products route:
Code snippet
routes.MapRoute(
name: "Products",
url: "products/view/{id}",
defaults: new { controller = "Products", action = "View", id = UrlParameter.Optional },
constraints: new { id = new PositiveIntegerConstraint() }
);
This will ensure that the value of the id parameter is always a positive integer.
Routing is a powerful feature of ASP.NET MVC that allows you to decouple the URLs in your application from the controller actions that are used to process those requests. This makes your application more flexible and easier to maintain.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
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.
ASP.NET MVC routing is a pattern matching system that is responsible for mapping incoming browser requests to specified MVC controller actions. When a user requests a URL, the routing engine matches the URL against the registered routes and executes the controller action that is associated with the matching route.
Routing is configured in the RouteConfig class, which is located in the App_Start folder of the application. The RouteConfig class contains a collection of Route objects, which define the URL patterns and controller actions that are associated with each route.
To create a new route, you need to specify the following information:
For example, the following code defines a route that maps the URL /products/view/ to the ProductsController class and the View action:
Code snippet
When a user requests the URL /products/view/1, the routing engine will match the URL against the Products route and execute the ProductsController.View action. The ProductsController.View action will then render the view for the product with the ID of 1.
ASP.NET MVC also supports custom routing constraints, which can be used to restrict the values that are allowed for route parameters. For example, the following code defines a custom routing constraint that ensures that the value of the id parameter is a positive integer:
The PositiveIntegerConstraint can then be used to restrict the values that are allowed for the id parameter in the Products route:
Code snippet
This will ensure that the value of the id parameter is always a positive integer.
Routing is a powerful feature of ASP.NET MVC that allows you to decouple the URLs in your application from the controller actions that are used to process those requests. This makes your application more flexible and easier to maintain.