dotnet / dotnet/aspnetcore

API Version Neutral Throwing 404 - DotNet 8.0 Upgrade

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

Description

In upgrading applications from DotNet 6.0 to DotNet 8.0 I encountered an issue with routing version neutrality.

My existing implementation has a neutral healthcheck controller that accomplishes the following:

1. HealthCheck endpoints resolve in swagger for each present version
2. HealthCheck endpoint is responsive at any agnostic version, or versions not explicitly in use by the application (example/api/v999/healthcheck)

Here is my current structure:
```
using Asp.Versioning;
using Microsoft.AspNetCore.Mvc;

namespace example.Controller
{
[ApiVersionNeutral]
[ApiController]
[Route("example/api/[controller]/[action]")]
public class HealthCheckController : ControllerBase
{
[HttpGet]
public IActionResult Heartbeat() => Ok("Beep");
}
}
```

In Swagger this appears as:
![Image](https://github.com/user-attachments/assets/5b9aeae9-b0da-4e64-a88b-03b7780f3f1f)

However, after the upgrade I can no longer target the healthcheck endpoint agnostically. For any version beyond what's in specific use by my application controllers I will receive a 404 not found.

This is an issue for service infrastructure that targets a v1/healthcheck on an application that no longer has v1 controllers.

I was able to overcome this behavior by modifying my route to {version:int}:

```
[ApiVersionNeutral]
[ApiController]
[Route("example/api/v{version:int}/[controller]/[action]")]
public class HealthCheckController : ControllerBase
{
[HttpGet]
public IActionResult Heartbeat() => Ok("Beep");
}
```

but this is undesirable as it breaks my swagger UI - expecting a parameter:

![Image](https://github.com/user-attachments/assets/5a8c52fc-0965-460f-8014-dc8b9208f886)

For reference here is how my application versioning is configured:

```
public static void ConfigureApiVersioning(this IServiceCollection services)
{
services.AddApiVersioning(
options =>
{
options.DefaultApiVersion = new ApiVersion(1, 0);
options.ReportApiVersions = true;
options.AssumeDefaultVersionWhenUnspecified = true;
options.ApiVersionReader = new UrlSegmentApiVersionReader();
})
.AddApiExplorer(
options =>
{
options.GroupNameFormat = "'v'VVV";
options.SubstituteApiVersionInUrl = true;
options.AddApiVersionParametersWhenVersionNeutral = true;
});
}
```

Where there any breaking changes around the ApiVersionNeutral attribute?

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.