Skip to content

Accessing Version Information

Chris Martinez edited this page Jan 23, 2019 · 3 revisions

All of the service API version information is accessible via extension methods. 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:

ASP.NET Web API

[ApiVersion( "1.0" )]
[ApiVersion( "2.0" )]
public class MyController : ApiController
{
    public IHttpActionResult Get()
    {
        var apiVersion = Request.GetRequestedApiVersion();
        return Ok();
    }

    // supported in 3.0+
    public IHttpActionResult Get( int id, ApiVersion apiVersion ) => Ok();
}

ASP.NET Core

[ApiVersion( "1.0" )]
[ApiVersion( "2.0" )]
[ApiController]
public class Controller : ControllerBase
{
    public IActionResult Get()
    {
        var apiVersion = HttpContext.GetRequestedApiVersion();
        return Ok();
    }

    // supported in 3.0+
    public IActionResult Get( int id, ApiVersion apiVersion ) => Ok();
}
Clone this wiki locally