liveBook · Manning

Microsoft.AspNetCore.Mvc.Versioning

As explained in Building Web APIs with ASP.NET Core

Microsoft.AspNetCore.Mvc.Versioning is a NuGet package that provides API-versioning features for ASP.NET Core applications.

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

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Walks through package installation, service registration, controller attributes, and Swagger configuration; notes rather than resolves Minimal API compatibility pitfalls.
Examples
Uses v1 and v2 board-game controllers, versioned error endpoints, and separate Swagger documents.
What it assumes
Assumes an ASP.NET Core project with Program.cs and introduces the required versioning registrations before using the attributes.
What you can do after
Apply it via NuGet and Program.cs configuration to expose and document versioned ASP.NET Core endpoints.
How it connects
Connects API versioning to URL routing, controller attributes, Minimal APIs, OpenAPI, and Swagger UI.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Web APIs with ASP.NET Core. The section links point to the parts of the book it draws on.