-
Notifications
You must be signed in to change notification settings - Fork 711
API Version Reader
The IApiVersionReader interface defines the behavior of how an API version is read in its raw, unparsed form from the current HTTP request. There are three methods for reading an API version provided out-of-the-box or you can implement your own. The default, configured API version reader is an instance of the QueryStringApiVersionReader class.
ASP.NET Web API
public interface IApiVersionReader
{
string Read( HttpRequestMessage request );
}
ASP.NET Core
public interface IApiVersionReader
{
string Read( HttpRequest request );
}
The QueryStringApiVersionReader reads the requested API version from the requested query string. The default query string parameter name is api-version. The constructor for this class accepts the name of a query string parameter so that an alternate query string parameter can be used.
// svc?api-version=2.0
services.AddApiVersioning( o => o.ApiVersionReader = new QueryStringApiVersionReader() );
// svc?v=2.0
services.AddApiVersioning(
o => o.ApiVersionReader = new QueryStringApiVersionReader( "v" ) );
The HeaderApiVersionReader reads the requested API version from a HTTP request header. There is no default or standard HTTP header. You must define which HTTP header name or names contain the API version information. This method of API versioning does not conform to the Microsoft REST Guidelines.
services.AddApiVersioning(
o => o.ApiVersionReader = new HeaderApiVersionReader( "api-version" ) );
The MediaTypeApiVersionReader reads the requested API version from a HTTP media type request header. The supported headers are Content-Type and Accept. If both headers are present, then Content-Type is preferred. If the Accept header specifies qualities, then the API version associated with the highest quality is selected. This behavior is independent of media type negotiation. The default media type parameter is "v", but you may specify an alternate name. This method of API versioning does not conform to the Microsoft REST Guidelines; however, it is generally accepted as one of the few, fully REST-compliant methods of versioning.
// Content-Type: application/json;v=2.0
services.AddApiVersioning( o => o.ApiVersionReader = new MediaTypeApiVersionReader() );
// Content-Type: application/json;version=2.0
services.AddApiVersioning(
o => o.ApiVersionReader = new MediaTypeApiVersionReader( "version" ) );
The QueryStringOrHeaderApiVersionReader reads the requested API version from the requested query string or a HTTP header. A value specified in the query string parameter takes precedence over a HTTP header. This implementation is useful for existing services that support API versioning, but are not compliant with the Microsoft REST Guidelines. The default query string parameter name is api-version. The constructor for this class accepts the name of a query string parameter so that an alternate query string parameter can be used. No HTTP headers are defined by default.
services.AddApiVersioning(
o => o.ApiVersionReader =
new QueryStringOrHeaderApiVersionReader()
{
Headers = { "x-ms-version" }
} );
Beginning in release 1.1.0 and later, multiple IApiVersionReader implementations are combined using composition instead of inheritance. For convenience, you can use the provided, static method ApiVersionReader.Combine to compose multiple API version reading styles.
services.AddApiVersioning(
o => o.ApiVersionReader = ApiVersionReader.Combine(
new QueryStringApiVersionReader(),
new HeaderApiVersionReader()
{
Headers = { "api-version" }
} ) );
- Home
- Quick Starts
- Version Format
- Version Discovery
- Version Policies
- How to Version Your Service
- API Versioning with OData
- Configuring Your Application
- Error Responses
- API Documentation
- Extensions and Customizations
- Known Limitations
- FAQ
- Examples