dotnet / dotnet/aspnetcore

ApiVersion status value causing routing issues (error 404)

Open
#59,054 3 comments 0 reactions 0 assignees View on GitHub
area-routing
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

Defining the following ApiVersion causes endpoints that use this version to not be accessible (Error 404)
ApiVersion: "2.0-preview" (Major: 2, Minor: 0, status: "preview")

### Expected Behavior

Endpoints for the ApiVersion are accessible, and actions can be called on it.

### Steps To Reproduce

1. Create a simple (default) ASP.NET Web Api project and enable Api versioning with URL segments (using the Asp.Versioning.Mvc Nuget package).
2. Add the following Route attribute on the default controller: `[Route("v{version:apiVersion}/[controller]/[action]")]`
3. Add the following ApiVersion attributes on the default controller:
- `[ApiVersion("1.0")]`
- `[ApiVersion("2.0-alpha")]`
- `[ApiVersion("2.0-preview")]`
4. Run the Api
5. Try to call a action of version "2.0-alpha" -> Works
6. Try to call a action of version "2.0-preview" -> Doesn't work (error 404)

### Exceptions (if any)

_No response_

### .NET Version

8.0.403

### Anything else?

After some early debugging, I came across the following method that seems to cause the issue: `Microsoft.AspNetCore.Routing.RoutePatternMatcher.MatchComplexSegmentCore`
More specifically the following portion:
```csharp
int indexOfLiteral;
if (part.IsLiteral)
{
var literal = (RoutePatternLiteralPart)part;
indexOfLiteral = requestSegment.Slice(0, startIndex).LastIndexOf(
literal.Content,
StringComparison.OrdinalIgnoreCase);
}
```

Here it seems that the wrong index is calculated for the "v" literal that prefixes the version in the URL in the case of ApiVersion "2.0-preview".
It uses the index of the "v" in the word "preview" instead of the "v" at the start of the URL parameter "v2.0-preview".

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.