Routing maps URLs to code—controller actions, Razor Pages, or minimal API delegates. Convention-based MVC routes use {controller}/{action}/{id?}; attribute routing uses [Route] and [HttpGet] on methods.
Convention route
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
/Products/Details/5 → ProductsController.Details(5)
Attribute routing (Web API)
[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase {
[HttpGet("{id:int}")]
public IActionResult Get(int id) => Ok(/* ... */);
}
Important interview questions and answers
- Q: Convention vs attribute routing?
A: Convention centralizes patterns in Program.cs; attributes colocate routes with actions—APIs favor attributes. - Q: Route constraints?
A:{id:int}rejects non-integers with 404—stronger than string parsing in the action.
Self-check
- Which URL hits HomeController.Index() with default route?
- What does [controller] token expand to?
Interview prep
- Convention vs attribute routing?
Convention centralizes patterns in Program.cs; attribute routes colocate with actions—Web APIs typically favor [Route] and [HttpGet] on methods.