liveBook · Manning

Segment-based uri versioning

As explained in Building Web APIs with ASP.NET Core

Segment-based URI versioning identifies API versions with distinct URL path segments containing the version identifier.

Segment-based URI versioning puts the API version in a URL path segment, such as `/v1/` or `/v2/`. Different versions can remain available at the same time, helping existing clients keep working when an endpoint changes. The trade-off is more routes and configuration to maintain. 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
});
 
// ...

Configure ASP.NET Core to read the version from the URL segment. The API explorer groups versions and substitutes the version into generated URLs. section 3.3.3

Give each board-game controller a versioned route such as `v{version:apiVersion}/[controller]`, then mark its supported version with `[ApiVersion("1.0")]` or `[ApiVersion("2.0")]`. Register separate Swagger documents and UI endpoints for v1 and v2 so their routes can be inspected independently. section 3.3.3

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Walks through URL-segment reader configuration, versioned controller routes, and Swagger setup rather than only defining the technique.
Examples
Uses PayPal v1 and v2 URLs and board-game controller routes to illustrate versioned paths.
What it assumes
Assumes familiarity with ASP.NET Core controllers and service configuration while defining path segments through examples.
What you can do after
Apply it via ASP.NET Core API versioning packages to expose and document v1 and v2 routes.
How it connects
Connects API versioning to breaking changes, routing, API explorer, and Swagger.

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.