Страница:
Versioning by Header
Страницы
API Documentation
API Explorer Options
API Version Conventions
API Version Reader
API Version Selector
API Versioning Options
API Versioning with OData
Accessing Version Information
Configuring Your Application
Controller Conventions
Custom API Version Format
Custom Attributes
Defining a Service Version
Deprecating a Service Version
Error Responses
Existing Services Quick Start
Existing Services
FAQ
Home
How to Version Your Service
Known Limitations
Migration
New Services Quick Start
OData Batching
OData Model Configurations
OData Model Substitution
OData Protocol Transitions
OData Query Options
OData Versioned Controllers
OData Versioned Metadata
Swashbuckle Integration
Third Party Extensions
Version Advertisement
Version Discovery
Version Format
Version Interleaving
Version Neutral
Version Policies
Versioned Clients
Versioned ODataModelBuilder
Versioning by Header
Versioning by Media Type
Versioning via the Query String
Versioning via the URL Path
2
Versioning by Header
Chris Martinez редактировал(а) эту страницу 2022-12-29 12:47:21 -08:00
While media type negotiation is the defined method in REST for reasoning about the content expectations between a client and server, any arbitrary HTTP header can also be used to drive API versioning.
Let's assume the following controllers are defined:
ASP.NET Web API
namespace Services.V1
{
[ApiVersion( 1.0 )]
[RoutePrefix( "api/helloworld" )]
public class HelloWorldController : ApiController
{
[Route]
public string Get() => "Hello world!";
}
}
namespace Services.V2
{
[ApiVersion( 2.0 )]
[RoutePrefix( "api/helloworld" )]
public class HelloWorldController : ApiController
{
[Route]
public string Get() => "Hello world!";
[Route]
public string Post( string text ) => text;
}
}
ASP.NET Core with MVC (Core)
namespace Services.V1
{
[ApiVersion( 1.0 )]
[ApiController]
[Route( "api/[controller]" )]
public class HelloWorldController : ControllerBase
{
[HttpGet]
public string Get() => "Hello world!";
}
}
namespace Services.V2
{
[ApiVersion( 2.0 )]
[ApiController]
[Route( "api/[controller]" )]
public class HelloWorldController : ControllerBase
{
[HttpGet]
public string Get() => "Hello world!";
[HttpPost]
public string Post( string text ) => text;
}
}
ASP.NET Core with MVC (Core)
var hello = app.NewVersionedApi();
hello.MapGet( "/helloworld", () => "Hello world!" ).HasApiVersion( 1.0 );
Configuration
ASP.NET Web API and ASP.NET Core would then change the default API version reader as follows:
.AddApiVersioning( options => options.ApiVersionReader = new HeaderApiVersionReader( "x-ms-version" ) );
This will allow clients to request a specific API version by the custom HTTP header x-ms-version
. For example:
GET api/helloworld HTTP/1.1
host: localhost
x-ms-version: 1.0
HTTP/1.1 200 OK
host: localhost
content-type: text/plain
content-length: 12
Hello world!
- 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