Skip to content

[Analyzer] Warn on ambiguous route patterns #45223

@captainsafia

Description

@captainsafia

Background and Motivation

ASP.NET Core throws an error if a URL maps to two routes. This is fine, but it can cause confusion with new ASP.NET Core devs who are use to method overloading in C#. New developers might think that minimal or action endpoints also support overloading based on the bound type.

app.MapGet("/product/{name}", (string name) => ...);
app.MapGet("/product/{id}", (int id) => ...);

For example, the routes above produces an ambiguous match because they have the same form from routing's perspective:

  1. A literal product segment
  2. An unconstrained route parameter segment.

It doesn't matter that the route parameters have different names or are bound to a minimal API method with different .NET types.

This limitation is easy to learn and fix. However, it's not obvious. The analyzer is aimed at new developers who I writing their first app. They can learn in the IDE instead of a runtime error.

Proposed Analyzer

An analyzer that looks at routes and figures out whether they are ambiguous at coding time. This analyzer will only detect obvious and easily detectable situations. There are still many ways to get ambiguous route errors that the analyzer won't address.

Analyzer Behavior and Message

The analyzer will only warn for ambiguous routes mapped in the same method (MapGet calls in a method could be conditional because they're in if statements) scope and using the same route builder. It won't attempt to detect matching routes across methods because they may be passed route builders created by Group("...").

The analyzer message is the same message that ASP.NET Core throws at runtime.

Route handler analyzer

  • Title: "Route conflict detected between route handlers"
  • Message: "Route '{0}' conflicts with another handler route. An HTTP request that matches multiple routes results in an ambiguous match error. Fix the conflict by changing the route's path, HTTP method, or route constraints."

MVC route analyzer

  • Title: "Route conflict detected between controller actions"
  • Message: "Route '{0}' conflicts with another action route. An HTTP request that matches multiple routes results in an ambiguous match error. Fix the conflict by changing the route's path, HTTP method, or route constraints."

Category

  • Design
  • Documentation
  • Globalization
  • Interoperability
  • Maintainability
  • Naming
  • Performance
  • Reliability
  • Security
  • Style
  • Usage

Severity Level

  • Error
  • Warning
  • Info
  • Hidden

Routing is complex. I (@JamesNK) feel more comfortable if we start this as a warning. If there are no false positives, then we can make it an error in the future.

Usage Scenarios

app.MapGet("/product/{name}", (string name) => ...);
app.MapGet("/product/{id}", (int id) => ...);

Risks

False positives. When in doubt, the analyzer shouldn't warn.

Metadata

Metadata

Assignees

Labels

analyzerIndicates an issue which is related to analyzer experienceapi-approvedAPI was approved in API review, it can be implemented

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions