ASP.NET Core MVC Interview Question #24
What is attribute routing in ASP.NET Core MVC?
MVC Fundamentals, Controllers, Views & Routing Junior Beginner
Quick Interview Answer
Attribute routing defines route templates directly on controllers or actions using attributes such as Route, HttpGet, and HttpPost. It provides precise control over URLs and is useful when an endpoint does not fit a global conventional pattern.
Detailed Explanation
Attribute routing defines route templates directly on controllers or actions using attributes such as Route, HttpGet, and HttpPost. It provides precise control over URLs and is useful when an endpoint does not fit a global conventional pattern.
Controller-level and action-level templates can be combined. Route names, constraints, and tokens can also be used when a more explicit routing design is required.
Code Example
[Route("products")]
public class ProductsController : Controller
{
[HttpGet("{id:int}")]
public IActionResult Details(int id)
{
return View();
}
}