liveBook · Manning

Overlapping route template

As explained in ASP.NET Core in Action, 3e

An overlapping route template is a route template whose matching rules also match one or more other route templates for at least one URL.

An overlapping route template matches at least one URL also matched by another template. That ambiguity can prevent routing from selecting a single endpoint. section 6.3.3

A URL matched twice

Suppose two endpoints use "/{number}/{name}" and "/{product}/{id}". The URL "/shoes/123" matches both, so the routing middleware throws an exception. Adding an integer constraint to the first template, "/{number:int}/{name}", removes that particular match, but "/123/shoes" still matches both. Prefer distinct route designs over relying on constraints to untangle ambiguous templates. section 6.3.3

Watch for overlapping templates when using Razor Pages conventions:

  • Pages/ToDo/Index.cshtml creates both "/ToDo" and "/ToDo/Index" routes. section 14.3
  • Adding Pages/ToDo.cshtml beside Pages/ToDo/Index.cshtml creates a collision at "/ToDo". section 14.3
  • Avoid unnecessary absolute paths, literal additions, and extra AddPageRoute() templates when they can duplicate existing conventions. section 14.6
Figure 14.4 If multiple Razor Pages are registered with overlapping route templates, you’ll get an exception at runtime when the router can’t work out which one to select.
CH14_F05_Lock3

Use the figure to see why multiple Razor Pages with overlapping templates fail at runtime when the router cannot choose one. section 14.6

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains matching examples, runtime failures, and constraint-based mitigation, while recommending distinct templates as the safer design.
Examples
Demonstrates competing parameterized templates, constrained parameters, and colliding ToDo Razor Page paths.
What it assumes
Assumes familiarity with URLs and endpoints while establishing route-template matching and Razor Pages conventions through examples.
What you can do after
Diagnose runtime routing exceptions and redesign ambiguous templates before relying on constraints.
How it connects
Connects route templates with route constraints, Razor Pages, minimal APIs, URL conventions, and runtime routing exceptions.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.