.Net Core
Routing

How does Routing work in ASP.NET Core?

Routing in ASP.NET Core is a mechanism that maps incoming HTTP requests to the appropriate endpoints in your application. It involves determining which code should handle a given request based on the URL and other request properties. Routing is a core feature of ASP.NET Core, enabling the application to respond to different URLs with different actions, controllers, or views.

1. Routing Basics

Routing works by defining patterns that match incoming request URLs to specific endpoints, which are usually action methods in controllers. ASP.NET Core uses a routing system that consists of the following key components:

  • Route Templates: Define patterns for URLs that the routing system should match.
  • Endpoint Routing: Determines which endpoint should handle the request based on the route pattern.
  • Route Constraints: Apply additional conditions to route matching, such as data types or specific values.

2. Route Templates

Route Templates are used to define patterns for URLs. They can include parameters and constraints.

Example:

In Startup.cs, you configure routes using MapControllerRoute:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
        app.UseHsts();
    }
 
    app.UseHttpsRedirection();
    app.UseStaticFiles();
 
    app.UseRouting();
 
    app.UseAuthorization();
 
    app.UseEndpoints(endpoints =>
    {
        // Default route pattern: /{controller}/{action}/{id?}
        endpoints.MapControllerRoute(
            name: "default",
            pattern: "{controller=Home}/{action=Index}/{id?}");
    });
}

Real-Life Analogy: Think of routing like a postal system. The routing template is akin to the address format you use on a letter. When a letter is sent, the postal system (routing) reads the address (URL pattern) and delivers it to the correct destination (controller/action).

3. Endpoint Routing

Endpoint Routing is the process by which the routing system determines which endpoint (e.g., controller action or Razor page) should handle a given request. ASP.NET Core’s routing uses a routing table to match incoming requests to endpoints based on route patterns.

Example:

Controller:

public class ProductsController : Controller
{
    public IActionResult List()
    {
        // Returns a view showing a list of products
        return View();
    }
 
    public IActionResult Details(int id)
    {
        // Returns a view showing details of a product
        return View();
    }
}

Route Definitions:

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute(
        name: "productList",
        pattern: "products/list",
        defaults: new { controller = "Products", action = "List" });
 
    endpoints.MapControllerRoute(
        name: "productDetails",
        pattern: "products/details/{id?}",
        defaults: new { controller = "Products", action = "Details" });
});

Real-Life Analogy: Imagine a directory in a large building with different offices. The directory (routing) has a map (route patterns) indicating which office (endpoint) is located in which section of the building. When you find the correct section (URL), you are directed to the specific office (controller/action) that you need.

4. Route Constraints

Route Constraints are used to restrict the matching of routes based on additional criteria such as data types or specific values.

Example:

Route Definition with Constraints:

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute(
        name: "productDetails",
        pattern: "products/details/{id:int}",
        defaults: new { controller = "Products", action = "Details" });
});

In this example, the route will only match if the id parameter is an integer.

Real-Life Analogy: Consider a restricted-access area in a building that requires a specific badge (constraint) to enter. Only those with the correct badge (data type) can enter, similar to how route constraints ensure that only requests meeting certain criteria are matched to a route.

5. Attribute Routing

Attribute Routing allows you to specify routes directly on controller actions using attributes, providing more flexibility and readability for defining routes.

Example:

Controller with Attribute Routing:

[Route("products")]
public class ProductsController : Controller
{
    [HttpGet("list")]
    public IActionResult List()
    {
        return View();
    }
 
    [HttpGet("details/{id}")]
    public IActionResult Details(int id)
    {
        return View();
    }
}

Real-Life Analogy: Think of attribute routing like having specific signs on different rooms in a hotel. Each room has a sign (attribute) indicating its function and the path to it, making it easy to navigate directly to the room you need without consulting a central directory.

6. Route Data and Values

Route Data and Route Values are used to pass parameters from the URL to the controller action.

Example:

Controller Action:

public class ProductsController : Controller
{
    public IActionResult Details(int id)
    {
        // id is retrieved from the route data
        var product = _productService.GetProductById(id);
        return View(product);
    }
}

Route Definition:

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute(
        name: "productDetails",
        pattern: "products/details/{id?}",
        defaults: new { controller = "Products", action = "Details" });
});

Real-Life Analogy: When visiting a museum, you might use a map (route data) to locate specific exhibits (controller actions). The map (URL pattern) provides information on which exhibit (parameter) you’re interested in, guiding you to the right location.

7. Conclusion

Routing in ASP.NET Core is a fundamental feature that maps HTTP requests to the appropriate controller actions or endpoints based on URL patterns. By understanding and using routing effectively, you can create well-structured, maintainable applications with clear URL patterns and dynamic handling of requests. Routing's flexibility, including route templates, endpoint routing, constraints, and attribute routing, allows for robust URL management and efficient request handling in your applications.