dotnet / dotnet/aspnetcore

AmbiguousMatchException thrown when using required route constraint

Open
#62,278 14 comments 0 reactions 0 assignees View on GitHub
area-mvc
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

### Describe the bug

I have two routes similar routes where I use route constraints to evade ambiguity:

Route 1:
```csharp
[HttpGet]
[Route("test/{name}")]
public IActionResult TestByName(string name) { return Ok(); }
```

Route 2 (with guid):
```csharp
[HttpGet]
[Route("test/{id:guid}")]
public IActionResult TestById(Guid id) { return Ok(); }
```

This works as expected. If I now add the `required` route constraint (see [documentation](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-8.0#route-constraints)) to both routes it will throw a `Microsoft.AspNetCore.Routing.Matching.AmbiguousMatchException`:

Route 1:
```csharp
[HttpGet]
[Route("test/{name:required}")]
public IActionResult TestByName(string name) { return Ok(); }
```

Route 2 (with guid):
```csharp
[HttpGet]
[Route("test/{id:guid:required}")]
public IActionResult TestById(Guid id) { return Ok(); }
```

### Expected Behavior

It should not throw a `Microsoft.AspNetCore.Routing.Matching.AmbiguousMatchException`.

### Steps To Reproduce

See above.

### Exceptions (if any)

Microsoft.AspNetCore.Routing.Matching.AmbiguousMatchException

### .NET Version

8.0.410

### Anything else?

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.