MapToApiVersion attribute not working
ammar-haider opened this issue · comments
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
the following code works fine when version 1.0 is specified but does not work when version 2.0 is specified in the request while using Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer version 5.1.0 but requests with bother versions work fine with version 5.0.0. With 5.1.0 it throws unsupportedapiversion when v2.0 is specified in the request.
`using Microsoft.AspNetCore.Mvc;
namespace MyApi.Controllers.V1
{
[ApiController]
[ApiVersion("1.0", Deprecated = true)]
[ApiExplorerSettings(GroupName = "General")]
[Route("api/v{version:apiVersion}/[controller]/[action]")]
public class MyController : ControllerBase
{
[HttpGet]
[MapToApiVersion("1.0")]
[MapToApiVersion("2.0")]
public IActionResult GetV1()
{
// Handle both API versions 1.0 and 2.0
}
}
}`
Expected Behavior
should work when either of the versions (1.0 or 2.0) is specified in the request.
Steps To Reproduce
No response
Exceptions (if any)
unsupportedapiversion exception is thrown when v2.0 is specified in the request.
.NET Version
7.0
Anything else?
No response
Mapping an API version is different from declaring it. [MapToApiVersion]
never declares an API version; it only maps it for the purposes of dispatch. There was an old bug that allowed routing to an API version what was mapped only. This was reported in #735 and the fix was first released in 5.1
.
This is the correct behavior for 5.1.x
. Starting in 6.0
, when an API version is missed this way and you are versioning by URL segment, the client receives 404 instead of 400. The response is otherwise unchanged.
Finally, I should call out that 5.x
is not officially supported for .NET 7. You should considering moving to the new Asp.Versioning.Mvc package. With the exception of the package name change, namespace change, and slightly different DI configuration, most things are largely the same. Most people have reported low friction in migrating.
This thread has gone idle. I presume you eventually got things working again. Relying on the way [MapToApiVersion]
was working was wrong and that behavior will not come back. If you have more questions about how to achieve a particular behavior, I'm happy to answer them.