- Notifications
You must be signed in to change notification settings - Fork710
API Version Reader
TheIApiVersionReader
interface defines the behavior of how an API version is read in its raw, unparsed form from the current HTTP request. There are multiple methods for reading an API version provided out-of-the-box or you can implement your own. The default, configured API version reader is a composed instanceQueryStringApiVersionReader
andUrlSegmentApiVersionReader
.
publicinterfaceIApiVersionReader{IReadOnlyList<string>Read(HttpRequestMessagerequest);}
publicinterfaceIApiVersionReader{IReadOnlyList<string>Read(HttpRequestrequest);}
TheQueryStringApiVersionReader
reads the requested API version from the requested query string. The default query string parameter name isapi-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.0AddApiVersioning( options=>options.ApiVersionReader=newQueryStringApiVersionReader());
// svc?v=2.0AddApiVersioning( options=>options.ApiVersionReader=newQueryStringApiVersionReader("v"));
TheHeaderApiVersionReader
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 theMicrosoft REST Guidelines.
AddApiVersioning( options=>options.ApiVersionReader=newHeaderApiVersionReader("api-version"));
TheMediaTypeApiVersionReader
reads the requested API version from a HTTP media type request header. The supported headers areContent-Type andAccept. If both headers are present, thenContent-Type is preferred. If theAccept 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 theMicrosoft REST Guidelines; however, it is generally accepted as a fully REST-compliant method of versioning.
// Content-Type: application/json;v=2.0AddApiVersioning( options=>options.ApiVersionReader=newMediaTypeApiVersionReader());
// Content-Type: application/json;version=2.0AddApiVersioning( options=>options.ApiVersionReader=newMediaTypeApiVersionReader("version"));
TheMediaTypeApiVersionReaderBuilder
is also available with additional features that allow:
- Define multiple media type parameters
- Mutually include specific media types
- Mutually exclude specific media types
- Match media types by template
- Match media types by pattern
- Disambiguate between multiple API versions
// Accept: application/json;v=2.0AddApiVersioning( options=>{varbuilder=newMediaTypeApiVersionReaderBuilder();options.ApiVersionReader=builder.Parameter("v").Include("application/json").Build();});
// Accept: application/vnd.my.company.v1+jsonAddApiVersioning( options=>{varbuilder=newMediaTypeApiVersionReaderBuilder();options.ApiVersionReader=builder.Template("application/vnd.my.company.v{version}+json").Build();});
MultipleIApiVersionReader
implementations can be combined using composition instead of inheritance. For convenience, you can useApiVersionReader.Combine
to compose multiple API version reading styles.
AddApiVersioning( options=>options.ApiVersionReader=ApiVersionReader.Combine(newQueryStringApiVersionReader(),newHeaderApiVersionReader(){HeaderNames={"x-ms-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