Skip to content

ApiVersion status value causing routing issues (error 404) #59054

@PascalWillems

Description

@PascalWillems

Is there an existing issue for this?

  • 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")]
  1. Run the Api
  2. Try to call a action of version "2.0-alpha" -> Works
  3. 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:

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".

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions