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 via the Query String

Chris Martinez edited this pageDec 29, 2022 ·5 revisions

The initial version of a controller may not have any API version attribution and will implicitly become the configured default API version. The default configuration uses the value1.0.

ASP.NET Web API

[RoutePrefix("api/helloworld")]publicclassHelloWorldController:ApiController{[Route]publicstringGet()=>"Hello world!";}

ASP.NET Web API and OData

[ODataRoutePrefix("People")]publicclassPeopleController:ODataController{[ODataRoute]publicIHttpActionResultGet(ODataQueryOptions<Person>options)=>Ok(new[]{newPerson()});}

ASP.NET Core with MVC (Core)

[ApiController][Route("api/[controller]")]publicclassHelloWorldController:ControllerBase{[HttpGet]publicstringGet()=>"Hello world!";}

ASP.NET Core and OData

publicclassPeopleController:ODataController{[HttpGet]publicIHttpActionResultGet(ODataQueryOptions<Person>options)=>Ok(new[]{newPerson()});}

To create the next version of the controller, you can choose to create a new controller with the same route, but decorated it as API version2.0. For example:

ASP.NET Web API

[ApiVersion(2.0)][RoutePrefix("api/helloworld")]publicclassHelloWorldController:ApiController{[Route]publicstringGet()=>"Hello world!";}

ASP.NET Web API and OData

[ApiVersion(2.0)][ControllerName("People")][ODataRoutePrefix("People")]publicclassPeople2Controller:ODataController{[ODataRoute]publicIHttpActionResultGet(ODataQueryOptions<Person>options)=>Ok(new[]{newPerson()});}

ASP.NET Core with MVC (Core)

[ApiVersion(2.0)][ApiController][Route("api/helloworld")]publicclassHelloWorld2Controller:ControllerBase{[HttpGet]publicstringGet()=>"Hello world!";}

ASP.NET Core and OData

[ApiVersion(2.0)][ControllerName("People")]publicclassPeople2Controller:ODataController{[HttpGet]publicIHttpActionResultGet(ODataQueryOptions<Person>options)=>Ok(new[]{newPerson()});}

The effect of this attribution is that the following requests match different controller implementations:

Request URLMatched Controller
/api/helloworld?api-version=1.0HelloWorldController
/api/helloworld?api-version=2.0HelloWorld2Controller
/api/People?api-version=1.0PeopleController
/api/People?api-version=2.0People2Controller

It’s important to note that only an undecorated controller will be inferred as the configured, default API version. Once a controller has any API version attribution, it will never be considered as the default API version again unless the API version attribute includes the default API version. This allows you permanently remove API versions over time.

ASP.NET Core with Minimal APIs

varbuilder=WebApplication.CreateBuilder(args);builder.Services.AddProblemDetails();builder.Services.AddApiVersioning();varapp=builder.Build();varhello=app.NewVersionedApi();varv1=hello.MapGroup("/helloworld").HasApiVersion(1.0);varv2=hello.MapGroup("/helloworld").HasApiVersion(2.0);v1.MapGet("/",()=>"Hello world!");v2.MapGet("/",()=>"Hello world!");app.Run();
Clone this wiki locally

[8]ページ先頭

©2009-2025 Movatter.jp