Routing in ASP.NET MVC is a mechanism that maps incoming HTTP requests to specific controller actions. With routing, you can define the URL patterns that correspond to your application's functionalities. Here is a basic overview of how routing works in ASP.NET MVC and the types of routing you may implement.

Routing in ASP.NET MVC

  1. Default Route: When you create a new ASP.NET MVC project, a default route is defined in the RouteConfig.cs file located in the App_Start folder. The default route typically looks like this:

    csharp
    1public 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.
  2. Registering Routes: You need to call the RegisterRoutes method in the Application_Start method in Global.asax.cs:

    csharp
    1protected void Application_Start() 2{ 3 AreaRegistration.RegisterAllAreas(); 4 RouteConfig.RegisterRoutes(RouteTable.Routes); 5}

Types of Routing in ASP.NET MVC

  1. 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.

  2. 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 the RegisterRoutes method:

    csharp
    1public 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:

    csharp
    1public 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}
  3. Custom Routes: You can create custom routes by defining your own URL patterns. For example:

    csharp
    1routes.MapRoute( 2 name: "CustomRoute", 3 url: "products/{category}/{id}", 4 defaults: new { controller = "Products", action = "Index", id = UrlParameter.Optional } 5);
  4. 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:

    csharp
    1routes.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);
  5. 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:

    csharp
    1routes.MapRoute( 2 name: "CatchAll", 3 url: "{*url}", 4 defaults: new { controller = "Home", action = "NotFound" } 5);

Summary

ishort, 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.

Leave a Reply

Your email address will not be published. Required fields are marked *


Talk to us?

Post your blog

F.A.Q

Frequently Asked Questions