Microsoft.AspNetCore.Mvc.Versioning is a NuGet package for assigning versions to ASP.NET Core API endpoints. Install it alongside Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer when versioned routes also need to appear in API documentation. section 3.3.3
// ...
builder.Services.AddApiVersioning(options => {
options.ApiVersionReader = new UrlSegmentApiVersionReader(); #1
options.AssumeDefaultVersionWhenUnspecified = true;
options.DefaultApiVersion = new ApiVersion(1, 0);
});
builder.Services.AddVersionedApiExplorer(options => {
options.GroupNameFormat = "'v'VVV"; #2
options.SubstituteApiVersionInUrl = true; #3
});
// ...
In Program.cs, register API versioning with a URL-segment reader and a default version of 1.0. Register the versioned API explorer to group endpoints and substitute their versions into URLs. section 3.3.3
Versioned board-game routes
Two BoardGames controllers can use the route v{version:apiVersion}/[controller], with [ApiVersion("1.0")] on one and [ApiVersion("2.0")] on the other. Configure separate v1 and v2 Swagger documents and UI endpoints so both versions can be inspected. section 3.3.3
