Conventional Routing vs Attribute Routing
| Feature | Conventional Routing | Attribute Routing |
|---|---|---|
| Definition | Routes are defined centrally | Routes are defined on controllers and actions |
| Configuration location | Program.cs |
Controller/action attributes |
| Example | {controller=Home}/{action=Index}/{id?} |
[Route("products/details/{id}")] |
| Route control | Follows a common URL pattern | Provides precise control for each endpoint |
| Best suited for | Traditional MVC web applications | Web APIs and custom URLs |
| Maintenance | Easy when controllers follow one pattern | Easy to understand near each action |
| Route changes | Centralized | Changed in individual controllers/actions |
| Route names | Often based on controller and action names | Can be independent of method names |
Conventional Routing Example
Configured in Program.cs:
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
Controller:
public class ProductsController : Controller
{
public IActionResult Details(int id)
{
return View();
}
}
URL:
/Products/Details/10
Attribute Routing Example
[Route("products")]
public class ProductsController : Controller
{
[HttpGet("details/{id:int}")]
public IActionResult Details(int id)
{
return View();
}
}
URL:
/products/details/10
Key Points
- Conventional routing uses a globally defined route pattern.
- Attribute routing places route templates directly on controllers and actions.
- Conventional routing is commonly used for MVC applications with consistent URLs.
- Attribute routing is preferred when endpoints require customized URLs.
- Both routing styles can be used in the same application.
Yes, Attribute Routing Can Be Used in ASP.NET Core MVC
Attribute routing works with both ASP.NET Core MVC controllers and Web API controllers.
Example
[Route("products")]
public class ProductsController : Controller
{
[HttpGet("")]
public IActionResult Index()
{
return View();
}
[HttpGet("details/{id:int}")]
public IActionResult Details(int id)
{
return View();
}
}
URLs
/products
/products/details/10
Program.cs
Register MVC:
builder.Services.AddControllersWithViews();
Map controllers:
app.MapControllers();
If the application also uses conventional routing:
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
Key Points
- MVC Core supports attribute routing.
- Use
[Route],[HttpGet],[HttpPost],[HttpPut]and[HttpDelete]. - Attribute and conventional routing can coexist.
- Attribute routing is useful for custom and readable URLs.
- For MVC form actions, use
[HttpPost]and anti-forgery validation.
[HttpPost("create")] [ValidateAntiForgeryToken] public IActionResult Create(Product model) { return View(model); }
How Does Endpoint Routing Work?
Endpoint routing matches an incoming HTTP request to an application endpoint.
An endpoint can be:
- MVC controller action
- Razor Page
- Minimal API handler
- SignalR Hub
Request Flow
HTTP Request
↓
UseRouting()
↓
Find Matching Endpoint
↓
Authentication and Authorization
↓
Execute Endpoint
↓
HTTP Response
MVC Configuration
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllersWithViews();
var app = builder.Build();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();
How It Works
UseRouting()examines the request URL and HTTP method.- It compares the request with registered route patterns.
- A matching endpoint is selected.
- Endpoint metadata becomes available, such as authorization requirements.
- Authentication and authorization middleware check access.
- The selected controller action or handler is executed.
- If no endpoint matches, the application normally returns 404 Not Found.
Example
Request:
GET /Products/Details/10
Route:
"{controller=Home}/{action=Index}/{id?}"
Matched values:
Controller = Products
Action = Details
ID = 10
Selected endpoint:
ProductsController.Details(10)
Key Points
- Endpoint routing selects the code that handles the request.
- It supports both conventional and attribute routing.
- Route constraints such as
{id:int}restrict which URLs can match. MapControllerRoute()registers conventional MVC endpoints.MapControllers()registers attribute-routed controller endpoints.- Routing should happen before authorization.
- No matching endpoint normally results in HTTP 404.