ASP.NET Core Interview Question #104
What is the difference between conventional routing and attribute routing?
Routing, Controllers & Model Binding Mid-Level Intermediate
Quick Interview Answer
Conventional routing defines general route templates in one central location and relies on controller/action conventions. Attribute routing places route templates directly on controllers and actions.
Detailed Explanation
Conventional routing defines general route templates in one central location and relies on controller/action conventions. Attribute routing places route templates directly on controllers and actions.
Conventional routing is concise for traditional MVC sites with predictable URL patterns. Attribute routing gives each endpoint explicit control and is often preferred for APIs or specialized URLs.
Both styles can exist in the same application when there is a clear reason, but route design should remain predictable and avoid unnecessary ambiguity.
Code Example
// Conventional
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
// Attribute
[HttpGet("products/{id:int}")]
public IActionResult GetProduct(int id) => Ok();