---
title: "How does routing work in ASP.NET MVC? Explain attribute routing."  
description: "How does routing work in ASP.NET MVC? Explain attribute routing."  
author: "Anubhav Sharma"  
published: 2025-06-11  
updated: 2025-06-19  
canonical: https://www.mindstick.com/forum/161708/how-does-routing-work-in-asp-dot-net-mvc-explain-attribute-routing  
category: "c#"  
tags: ["c#"]  
reading_time: 3 minutes  

---

# How does routing work in ASP.NET MVC? Explain attribute routing.

**How does [routing work in ASP.NET](https://www.mindstick.com/forum/158586/how-does-routing-work-in-asp-dot-net-mvc-and-how-is-it-configured) [MVC](https://www.mindstick.com/forum/155803/define-cache-profile-in-mvc)? [Explain](https://www.mindstick.com/forum/157854/what-is-system-debugging-explain-some-system-debugging-tools-used-in-modern-computer-systems) [attribute routing](https://www.mindstick.com/interview/34238/how-does-routing-work-in-asp-dot-net-mvc-explain-attribute-routing).**

## Replies

### Reply by ICSM Computer

In **[ASP.NET MVC](https://www.mindstick.com/forum/155798/what-is-caching-in-asp-dot-net-mvc)**, **[routing](https://www.mindstick.com/articles/11998/attribute-routing-in-asp-dot-net-mvc)** 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:

```cs
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:

1. MVC reads the routing table in order.
2. It matches the request URL to the defined route pattern.
3. It determines:

   1. Which controller to instantiate.
   2. Which action method to invoke.
   3. What parameters to pass.

## What is Attribute Routing?

**[Attribute](https://www.mindstick.com/blog/221/attributes-reflection) Routing** lets you define routes **directly on controller actions** using `[Route]` attributes instead of relying on the centralized route table.

> Introduced in **ASP.NET MVC 5**.

### Enabling Attribute Routing

In `RouteConfig.cs`:

```cs
public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    // Enable attribute routing
    routes.MapMvcAttributeRoutes();

    // (Optional) fallback conventional route
    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}
```

### Example: Attribute Routing

```cs
public class ArticlesController : Controller
{
    [Route("articles/view/{id}")]
    public ActionResult ViewArticle(int id)
    {
        // Logic
        return View();
    }

    [Route("articles/delete/{id}")]
    public ActionResult DeleteArticle(int id)
    {
        // Logic
        return View();
    }
}
```

**URL:** `/articles/view/10` → hits `ViewArticle(10)`\
**URL:** `/articles/delete/10` → hits `DeleteArticle(10)`

### Attribute Routing with Constraints

```cs
[Route("blog/{year:int}/{month:int}/{title}")]
public ActionResult BlogPost(int year, int month, string title) { ... }
```

Adds validation to ensure only integers for `year` and `month`.

## Benefits of Attribute Routing

| Feature | Benefit |
| --- | --- |
| Clarity | Routes are defined next to the action methods |
| Flexibility | Easily create custom and SEO-friendly URLs |
| Control | No need to rely on one global route template |
| Support | Route constraints, optional params, defaults |

## Summary

| Routing Type | Description |
| --- | --- |
| **Convention-based** | Central route pattern (e.g., `{controller}/{action}/{id}`) |
| **Attribute routing** | Routes defined directly on actions using `[Route]` |

## Routing = URL → Controller + Action + Parameters


---

Original Source: https://www.mindstick.com/forum/161708/how-does-routing-work-in-asp-dot-net-mvc-explain-attribute-routing

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
