Requested API Version
All of the service API version information is accessible via extension methods and properties. Beginning in version
3.0, Model Binding is also supported. These features allow you to determine which API version was requested by a
client as well as determine which versions are supported and deprecated. The API versions provided are automatically
aggregated across all service implementations.
The most common usage is the current, client requested API version:
Minimal API
var builder = WebApplication.CreateBuilder( args );
builder.Services.AddProblemDetails();
builder.Services.AddApiVersioning().EnableApiVersionBinding();
var app = builder.Build();
var api = app.NewVersionedApi();
api.MapGet( "/", ( ApiVersion version ) => Results.Ok() )
.HasApiVersion( 1.0 )
.HasApiVersion( 2.0 );
app.Run();
MVC (Core)
[ApiVersion( 1.0 )]
[ApiVersion( 2.0 )]
[ApiController]
public class Controller : ControllerBase
{
public IActionResult Get()
{
var apiVersion = HttpContext.RequestedApiVersion;
return Ok();
}
// supported in 3.0+
public IActionResult Get( int id, ApiVersion apiVersion ) => Ok();
}