Routing in ASP.NET MVC
Default Route: When you create a new ASP.NET MVC project, a default route is defined in the
RouteConfig.cs
file located in theApp_Start
folder. The default route typically looks like this:csharp1public class RouteConfig 2{ 3 public static void RegisterRoutes(RouteCollection routes) 4 { 5 routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 6 7 routes.MapRoute( 8 name: "Default", 9 url: "{controller}/{action}/{id}", 10 defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 11 ); 12 } 13}
In this example, the URL pattern
{controller}/{action}/{id}
is defined, where:{controller}
is the name of the controller.{action}
is the name of the action method.{id}
is an optional parameter.
Registering Routes: You need to call the
RegisterRoutes
method in theApplication_Start
method inGlobal.asax.cs
:csharp1protected void Application_Start() 2{ 3 AreaRegistration.RegisterAllAreas(); 4 RouteConfig.RegisterRoutes(RouteTable.Routes); 5}
Types of Routing in ASP.NET MVC
Convention-Based Routing: This is the default routing mechanism in ASP.NET MVC. It uses a predefined pattern to map URLs to controller actions. The example above is a convention-based route.
Attribute Routing: This allows you to define routes directly on your controller actions using attributes. You can enable attribute routing by calling
routes.MapMvcAttributeRoutes()
in theRegisterRoutes
method:csharp1public class RouteConfig 2{ 3 public static void RegisterRoutes(RouteCollection routes) 4 { 5 routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 6 7 routes.MapMvcAttributeRoutes(); 8 9 routes.MapRoute( 10 name: "Default", 11 url: "{controller}/{action}/{id}", 12 defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 13 ); 14 } 15}
Example of attribute routing:
csharp1public class ProductsController : Controller 2{ 3 [Route("products/{id}")] 4 public ActionResult Details(int id) 5 { 6 // Action logic here 7 return View(); 8 } 9 10 [Route("products")] 11 public ActionResult Index() 12 { 13 // Action logic here 14 return View(); 15 } 16}
Custom Routes: You can create custom routes by defining your own URL patterns. For example:
csharp1routes.MapRoute( 2 name: "CustomRoute", 3 url: "products/{category}/{id}", 4 defaults: new { controller = "Products", action = "Index", id = UrlParameter.Optional } 5);
Route Constraints: You can add constraints to your routes to restrict the types of values that can be passed to the route parameters. For example, you can restrict the
id
parameter to be an integer:csharp1routes.MapRoute( 2 name: "ProductRoute", 3 url: "products/{id}", 4 defaults: new { controller = "Products", action = "Details" }, 5 constraints: new { id = @"\d+" } // id must be a number 6);
Catch-All Routes: You can create a catch-all route that matches any URL pattern. This is useful for handling 404 errors or custom routing logic:
csharp1routes.MapRoute( 2 name: "CatchAll", 3 url: "{*url}", 4 defaults: new { controller = "Home", action = "NotFound" } 5);
Summary
in short, routing in ASP.NET MVC enables you to define how URLs map with your application's controllers and actions. Through convention-based routing, attribute routing, custom routes, route constraints, and catch-all routes, you can create flexible user-friendly URLs for your web application.