ASP.NET Core MVC Interview Question #79
What are named routes and when are they useful?
Advanced Routing, Filters, Areas & Application Structure Mid-Level Intermediate
Quick Interview Answer
A named route gives a route a stable identifier that can be used for URL generation or redirects. This is useful when code should target a particular route definition rather than depend on controller/action discovery.
Detailed Explanation
A named route gives a route a stable identifier that can be used for URL generation or redirects. This is useful when code should target a particular route definition rather than depend on controller/action discovery.
Route names should be unique and meaningful. They are especially helpful when several route patterns could otherwise generate a valid URL for the same values.
Code Example
app.MapControllerRoute(
name: "product-details",
pattern: "shop/product/{id:int}",
defaults: new { controller = "Products", action = "Details" });
// Later:
return RedirectToRoute("product-details", new { id = 10 });