ASP.NET Core Interview Question #103
What is attribute routing?
Routing, Controllers & Model Binding Mid-Level Intermediate
Quick Interview Answer
Attribute routing defines routes directly on controllers and actions using attributes such as Route, HttpGet, HttpPost, HttpPut, and HttpDelete.
Detailed Explanation
Attribute routing defines routes directly on controllers and actions using attributes such as Route, HttpGet, HttpPost, HttpPut, and HttpDelete.
It provides precise control over URLs and is especially common in Web APIs. Route templates can include parameters and constraints and can be composed from controller-level and action-level attributes.
Attribute routing is useful when the public URL structure should not depend directly on controller and action names.
Code Example
[Route("products")]
public class ProductsController : Controller
{
[HttpGet("{id:int}")]
public IActionResult Details(int id)
{
return View();
}
}