dotnet web_framework ai_generated true

Microsoft.AspNetCore.Routing.Matching.AmbiguousMatchException: The request matched multiple endpoints.

ID: dotnet/ambiguous-match

Also available as: JSON · Markdown
88%Fix Rate
90%Confidence
50Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
8 active

Root Cause

AmbiguousMatchException occurs when ASP.NET Core routing cannot determine which endpoint should handle a request because multiple routes match the same URL pattern. Common with attribute routing, overlapping route templates, and controller action overloads. Fix by adding route constraints or making route templates unique.

generic

Workarounds

  1. 90% success Add route constraints to differentiate ambiguous endpoints
    Use route constraints to disambiguate: [HttpGet("{id:int}")] for integer IDs vs [HttpGet("{slug:alpha}")] for string slugs. Common constraints: :int, :guid, :alpha, :regex(). For complex cases, implement IRouteConstraint for custom matching logic
  2. 88% success Use [HttpGet], [HttpPost] method-specific attributes with unique templates
    Replace generic [Route] with HTTP method-specific attributes that have distinct templates: [HttpGet("items")] and [HttpGet("items/{id:int}")] instead of two [Route("items")] actions. Ensure each HTTP method + route template combination is unique across all controllers

Dead Ends

Common approaches that don't work:

  1. Adding [Route] attributes to every action with fully explicit paths 60% fail

    Over-specifying routes creates a maintenance burden and often introduces new ambiguities when combined with controller-level [Route] attributes. The resulting route paths become fragile and hard to reason about

  2. Removing [ApiController] attribute to disable automatic model binding and routing behavior 80% fail

    Removing [ApiController] disables important features like automatic 400 responses for invalid models, attribute routing requirement, and binding source inference. It does not fix the ambiguous route templates

Error Chain

Leads to:
Preceded by:
Frequently confused with: