Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Versioning by Header

Chris Martinez edited this pageDec 29, 2022 ·2 revisions

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

namespaceServices.V1{[ApiVersion(1.0)][RoutePrefix("api/helloworld")]publicclassHelloWorldController:ApiController{[Route]publicstringGet()=>"Hello world!";}}namespaceServices.V2{[ApiVersion(2.0)][RoutePrefix("api/helloworld")]publicclassHelloWorldController:ApiController{[Route]publicstringGet()=>"Hello world!";[Route]publicstringPost(stringtext)=>text;}}

ASP.NET Core with MVC (Core)

namespaceServices.V1{[ApiVersion(1.0)][ApiController][Route("api/[controller]")]publicclassHelloWorldController:ControllerBase{[HttpGet]publicstringGet()=>"Hello world!";}}namespaceServices.V2{[ApiVersion(2.0)][ApiController][Route("api/[controller]")]publicclassHelloWorldController:ControllerBase{[HttpGet]publicstringGet()=>"Hello world!";[HttpPost]publicstringPost(stringtext)=>text;}}

ASP.NET Core with MVC (Core)

varhello=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=newHeaderApiVersionReader("x-ms-version"));

This will allow clients to request a specific API version by the custom HTTP headerx-ms-version. For example:

GETapi/helloworld HTTP/1.1host: localhostx-ms-version: 1.0
HTTP/1.1 200 OKhost: localhostcontent-type: text/plaincontent-length: 12Hello world!
Clone this wiki locally

[8]ページ先頭

©2009-2025 Movatter.jp