ASP.NET Core MVC Interview Question #25

What is the difference between conventional routing and attribute routing?

MVC Fundamentals, Controllers, Views & Routing Junior Beginner

Quick Interview Answer

Conventional routing defines reusable URL patterns in the application's endpoint configuration and determines controller/action values from those patterns. Attribute routing places route templates on individual controllers and actions.

Detailed Explanation

Conventional routing defines reusable URL patterns in the application's endpoint configuration and determines controller/action values from those patterns. Attribute routing places route templates on individual controllers and actions.

Conventional routing is concise for a uniform MVC site, while attribute routing provides more explicit endpoint control. They can coexist in the same application. The choice should be based on URL requirements and maintainability rather than assuming one approach is always superior.

Code Example

// Conventional
app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

// Attribute
[HttpGet("products/{id:int}")]
public IActionResult Details(int id) => View();