ASP.NET Core MVC Interview Question #82

How does route order affect MVC endpoint matching?

Advanced Routing, Filters, Areas & Application Structure Mid-Level Intermediate

Quick Interview Answer

When multiple endpoints could match a request, routing uses endpoint precedence, order, constraints, and other metadata to select the best candidate. More specific route patterns generally take precedence over broader patterns.

Detailed Explanation

When multiple endpoints could match a request, routing uses endpoint precedence, order, constraints, and other metadata to select the best candidate. More specific route patterns generally take precedence over broader patterns.

Developers should avoid relying on fragile ordering when routes can instead be made unambiguous through clear templates and constraints. Ambiguous routes can cause runtime endpoint-selection errors.

Code Example

app.MapControllerRoute(
    name: "blog-post",
    pattern: "blog/{slug}",
    defaults: new { controller = "Blog", action = "Post" });

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