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 URL Path

Chris Martinez edited this pageDec 29, 2022 ·9 revisions

An alternate, but common, method of API versioning is to use a URL path segment. This approach does not allow implicitly matching the initial, default API version of a service; therefore, all API versions must be explicitly declared. In addition, the API version value specified for the URL segment must still conform to theversion format. Thev prefix isnot part of the API version, but may be included in route templates if you so desire.

Note: It is not possible to have a default API version for a URL path segment. This means that settingApiVersioningOptions.AssumedDefaultVersionWhenUnspecified is unlikely to have any affect when you use this method of versioning. For more information and possible solutions to address this scenario, refer to theknown limitations.

ASP.NET Web API

publicstaticclassWebApiConfig{publicstaticvoidConfiguration(HttpConfigurationconfiguration){varconstraintResolver=newDefaultInlineConstraintResolver(){ConstraintMap={["apiVersion"]=typeof(ApiVersionRouteConstraint)}};configuration.MapHttpAttributeRoutes(constraintResolver);configuration.AddApiVersioning();}}
[ApiVersion(1.0)][Route("api/v{version:apiVersion}/helloworld")]publicclassHelloWorldController:ApiController{publicstringGet()=>"Hello world!";}[ApiVersion(2.0)][ApiVersion(3.0)][Route("api/v{version:apiVersion}/helloworld")]publicclassHelloWorld2Controller:ApiController{publicstringGet()=>"Hello world v2!";[MapToApiVersion(3.0)]publicstringGetV3()=>"Hello world v3!";}

ASP.NET Web API and OData

Since the OData implementation uses convention-based routes under the hood, theApiVersionRouteConstraint is automatically added to all versioned OData routes when needed. The name of the constraint used in prefixes of OData routes must beapiVersion and cannot be changed.

publicstaticclassWebApiConfig{publicstaticvoidConfiguration(HttpConfigurationconfiguration){varmodelBuilder=newVersionedODataModelBuilder(configuration){ModelConfigurations={newPersonModelConfiguration()}};configuration.AddApiVersioning();configuration.MapVersionedODataRoutes("odata-bypath","api/v{apiVersion}",modelBuilder);}}
[ApiVersion(1.0)][ODataRoutePrefix("People")]publicclassPeopleController:ODataController{[EnableQuery][ODataRoute]publicIQueryable<Person>Get()=>new[]{newPerson()}.AsQueryable();}[ApiVersion(2.0)][ApiVersion(3.0)][ControllerName("People")][ODataRoutePrefix("People")]publicclassPeople2Controller:ODataController{[EnableQuery][ODataRoute]publicIQueryable<Person>Get()=>new[]{newPerson()}.AsQueryable();[EnableQuery][ODataRoute,MapToApiVersion(3.0)]publicIQueryable<Person>GetV3()=>new[]{newPerson()}.AsQueryable();}

ASP.NET Core with MVC (Core)

[ApiVersion(1.0)][ApiController][Route("api/v{version:apiVersion}/[controller]")]publicclassHelloWorldController:ControllerBase{[HttpGet]publicstringGet()=>"Hello world!";}[ApiVersion(2.0)][ApiVersion(3.0)][ApiController][Route("api/v{version:apiVersion}/helloworld")]publicclassHelloWorld2Controller:ControllerBase{[HttpGet]publicstringGet()=>"Hello world v2!";[HttpGet,MapToApiVersion(3.0)]publicstringGetV3()=>"Hello world v3!";}

ASP.NET Core with OData

[ApiVersion(1.0)]publicclassPeopleController:ODataController{[EnableQuery]publicIQueryable<Person>Get()=>new[]{newPerson()}.AsQueryable();}[ApiVersion(2.0)][ApiVersion(3.0)][ControllerName("People")]publicclassPeople2Controller:ODataController{[EnableQuery][ODataRoute]publicIQueryable<Person>Get()=>new[]{newPerson()}.AsQueryable();[EnableQuery][ODataRoute,MapToApiVersion(3.0)]publicIQueryable<Person>GetV3()=>new[]{newPerson()}.AsQueryable();}
varbuilder=WebApplication.CreateBuilder(args);builder.Services.AddControllers().AddOData();builder.Services.AddProblemDetails();builder.Services.AddApiVersioning().AddOData(    options=>{options.ModelBuilder.DefaultModelConfiguration=(builder,apiVersion,routePrefix)=>{builder.EntitySet<Person>("People");};options.AddRouteComponents("api/v{version:apiVersion}");});varapp=builder.Build();app.MapControllers();app.Run();

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

Request URLMatched ControllerMatched Action
/api/v1/helloworldHelloWorldControllerGet
/api/v2/helloworldHelloWorld2ControllerGet
/api/v3/helloworldHelloWorld2ControllerGetV3
/api/v1/PeoplePeopleControllerGet
/api/v2/PeoplePeople2ControllerGet
/api/v3/PeoplePeople2ControllerGetV3

ASP.NET Core with Minimal APIs

varbuilder=WebApplication.CreateBuilder(args);builder.Services.AddProblemDetails();builder.Services.AddApiVersioning();varapp=builder.Build();varpeople=app.NewVersionedApi();varv1=people.MapGroup("/people/v{version:apiVersion}").HasApiVersion(1.0);v1.MapGet("/",()=>new[]{newPerson()});app.Run();
Clone this wiki locally

[8]ページ先頭

©2009-2025 Movatter.jp