I've upgraded a project from .net core 3.1 to .net 8. I had to update packages from Microsoft.AspNetCore.Mvc.Versioning to Asp.Versioning.Mvc.
I use versioning in my routes:
[ApiController]
[ApiVersionNeutral]
[ApiConventionType(typeof(DefaultApiConventions))]
[Route("~/v{version:apiVersion}/[controller]")]
public class SampleController : ControllerBase
{
[HttpGet("{userId}", Name = "GetUser")]
public async Task<ActionResult<UserResponseModel>> GetUser([FromRoute] int userId)
{
// Do something
}
}
The versioning worked fine before the upgrade, but afterwards {version} is now a parameter (seen in Swagger)
/v{version}/GetUser/{userId}
If I change [ApiVersionNeutral] to [ApiVersion("1.0")] the endpoint is correct.
/v1/GetUser/{userId}
My config has, and always had, the correct options:
services.AddApiVersioning(c =>
{
c.ReportApiVersions = true;
c.DefaultApiVersion = new ApiVersion(1, 0);
c.AssumeDefaultVersionWhenUnspecified = true;
});
Any ides on why [ApiVersionNeutral] has this issue?
RESOLVED!
I found that I also need AddApiExplorer option AddApiVersionParametersWhenVersionNeutral = true