API versioning in ASP.NET Core β URL segment, query string, and header strategies with Swagger per-version UI using the official
Asp.Versioningpackage.
If this sample saved you time, consider joining our Patreon community. You'll get exclusive .NET tutorials, premium code samples, and early access to new content β all for the price of a coffee.
π Join CodingDroplets on Patreon
Prefer a one-time tip? Buy us a coffee β
- How to add API versioning to ASP.NET Core Web API using the official
Asp.VersioningNuGet package - Three versioning strategies: URL segment (
/api/v1/), query string (?api-version=1.0), and request header (X-Api-Version: 2.0) - How to set a default version so existing clients aren't broken
- How to report supported and deprecated versions via response headers
- How to configure Swagger/OpenAPI with one document per API version for a clean developer experience
- How to evolve your API (v1 β v2) without breaking existing consumers
- How to write integration tests for versioned endpoints using
WebApplicationFactory
HTTP Client
β
ββ GET /api/v1/products ββ URL Segment versioning
ββ GET /api/v1/products?api-version=1.0 ββ Query String versioning
ββ GET /api/v1/products + Header X-Api-Version: 1.0 ββ Header versioning
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββ
β ASP.NET Core Pipeline β
β β
β ApiVersionReader (URL + QueryString + Header) β
β β resolves to ApiVersion(major, minor) β
β βΌ β
β ApiVersioningMiddleware β
β β routes to the correct controller β
β βΌ β
β βββββββββββββββββββ¬ββββββββββββββββββββββββββ β
β β ProductsV1 β ProductsV2 β β
β β Controller β Controller β β
β β [ApiVersion β [ApiVersion β β
β β ("1.0")] β ("2.0")] β β
β β β β β
β β GET /products β GET /products β β
β β GET /products β GET /products?category= β β
β β /{id} β /{id} β β
β β β GET /products/categoriesβ β
β βββββββββββββββββββ΄ββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ
Swagger UI (v1 doc + v2 doc dropdown)
| Strategy | Example Request | When to Use |
|---|---|---|
| URL Segment | GET /api/v1/products |
Default choice β explicit, cacheable |
| Query String | GET /api/v1/products?api-version=1.0 |
Easy for quick testing / scripts |
| Header | GET /api/v1/products + X-Api-Version: 2.0 |
Clean URLs, internal/service-to-service APIs |
| API Version | Model Fields | Extra Endpoints |
|---|---|---|
| v1 | Id, Name, Price |
None |
| v2 | Id, Name, Price, Category, Stock |
GET /categories filter |
dotnet-api-versioning-aspnetcore/
β
βββ ApiVersioningDemo/ # Main Web API project
β βββ Controllers/
β β βββ V1/
β β β βββ ProductsV1Controller.cs # [ApiVersion("1.0")] β basic product list
β β βββ V2/
β β βββ ProductsV2Controller.cs # [ApiVersion("2.0")] β extended + category filter
β βββ Models/
β β βββ ProductV1.cs # Id, Name, Price
β β βββ ProductV2.cs # Id, Name, Price, Category, Stock
β βββ Properties/
β β βββ launchSettings.json # Swagger opens automatically in Visual Studio
β βββ Program.cs # DI setup: versioning, Swagger, JSON options
β βββ appsettings.json
β βββ ApiVersioningDemo.csproj
β
βββ ApiVersioningDemo.Tests/ # xUnit integration tests
β βββ ProductsApiTests.cs # 12 tests β URL, query string, header versioning
β βββ ApiVersioningDemo.Tests.csproj
β
βββ dotnet-api-versioning-aspnetcore.sln # Solution file
βββ README.md
| Requirement | Version |
|---|---|
| .NET SDK | 10.0+ |
| IDE (optional) | Visual Studio 2022+, VS Code, or Rider |
Check your SDK version:
dotnet --version# 1. Clone the repository
git clone https://github.com/codingdroplets/dotnet-api-versioning-aspnetcore.git
cd dotnet-api-versioning-aspnetcore
# 2. Build the solution
dotnet build -c Release
# 3. Run the Web API
cd ApiVersioningDemo
dotnet run
# 4. Open Swagger UI in your browser
# http://localhost:5289/swaggerSwagger UI will open with a version dropdown β switch between v1 and v2 to explore each API independently.
<PackageReference Include="Asp.Versioning.Http" Version="8.1.0" />
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.1.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.3.1" />builder.Services.AddApiVersioning(options =>
{
options.DefaultApiVersion = new ApiVersion(1, 0);
options.AssumeDefaultVersionWhenUnspecified = true;
options.ReportApiVersions = true; // emits api-supported-versions header
options.ApiVersionReader = ApiVersionReader.Combine(
new UrlSegmentApiVersionReader(), // /api/v1/products
new QueryStringApiVersionReader("api-version"), // ?api-version=1.0
new HeaderApiVersionReader("X-Api-Version") // X-Api-Version: 2.0
);
})
.AddApiExplorer(options =>
{
options.GroupNameFormat = "'v'VVV"; // formats as v1, v2
options.SubstituteApiVersionInUrl = true;
});// V1 controller β serves /api/v1/products
[ApiController]
[ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/[controller]")]
public class ProductsController : ControllerBase { ... }
// V2 controller β serves /api/v2/products
[ApiController]
[ApiVersion("2.0")]
[Route("api/v{version:apiVersion}/[controller]")]
public class ProductsController : ControllerBase { ... }builder.Services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo { Title = "API Demo", Version = "v1" });
options.SwaggerDoc("v2", new OpenApiInfo { Title = "API Demo", Version = "v2" });
});app.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
options.SwaggerEndpoint("/swagger/v2/swagger.json", "v2");
});// v1 β simple
public class ProductV1
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public decimal Price { get; set; }
}
// v2 β extended (Category + Stock added without breaking v1)
public class ProductV2
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public decimal Price { get; set; }
public string Category { get; set; } = string.Empty;
public int Stock { get; set; }
}| Method | Endpoint | Description | Status Codes |
|---|---|---|---|
| GET | /api/v1/products |
Returns all products (basic) | 200 |
| GET | /api/v1/products/{id} |
Returns product by ID | 200, 404 |
| Method | Endpoint | Description | Status Codes |
|---|---|---|---|
| GET | /api/v2/products |
Returns all products (extended) | 200 |
| GET | /api/v2/products?category=X |
Filter products by category | 200 |
| GET | /api/v2/products/{id} |
Returns extended product by ID | 200, 404 |
| GET | /api/v2/products/categories |
Returns distinct category list | 200 |
| Method | Example | Description |
|---|---|---|
| GET | /api/v1/products?api-version=1.0 |
Query string versioning |
| GET | /api/v2/products?api-version=2.0 |
Query string versioning |
| GET | /api/v1/products + X-Api-Version: 1.0 |
Header versioning |
Response headers returned by all versioned endpoints:
api-supported-versions: 1.0, 2.0
dotnet test -c ReleaseTest Summary:
| Test Name | Strategy | Pass/Fail |
|---|---|---|
| V1_GetAll_Returns200_WithProducts | URL Segment | β Pass |
| V1_GetById_ExistingId_Returns200 | URL Segment | β Pass |
| V1_GetById_NonExistingId_Returns404 | URL Segment | β Pass |
| V1_GetAll_ResponseContainsApiSupportedVersionsHeader | URL Segment | β Pass |
| V2_GetAll_Returns200_WithExtendedProducts | URL Segment | β Pass |
| V2_GetAll_FilterByCategory_ReturnsMatchingProducts | URL Segment | β Pass |
| V2_GetById_ExistingId_Returns200_WithCategoryAndStock | URL Segment | β Pass |
| V2_GetById_NonExistingId_Returns404 | URL Segment | β Pass |
| V2_GetCategories_Returns200_WithDistinctCategories | URL Segment | β Pass |
| QueryStringVersion_V1_Returns200 | Query String | β Pass |
| QueryStringVersion_V2_Returns200 | Query String | β Pass |
| HeaderVersion_V1_Returns200 | Header | β Pass |
Total: 12 tests β 12 passed β
| Problem (No Versioning) | Solution (With Versioning) |
|---|---|
| Adding a field breaks existing clients | Old clients keep using v1; new clients adopt v2 |
| Removing a field causes runtime errors | Deprecate v1 gradually; remove only after migration |
| Behaviour changes break integrations | Each version is an isolated, stable contract |
| Strategy | Pros | Cons |
|---|---|---|
| URL Segment | Explicit, cacheable, easy to bookmark | Changes URL structure |
| Query String | No URL change, easy for testing | Not REST-pure, can pollute query params |
| Header | Clean URLs, good for service-to-service | Less discoverable, harder to test in browser |
Recommendation: Use URL segment as the primary strategy. Combine with query string and header for flexibility β exactly what this sample demonstrates.
When enabled, every response includes:
api-supported-versions: 1.0, 2.0
api-deprecated-versions: (empty if none deprecated)
This is invaluable for clients to self-discover which versions are available.
| Technology | Purpose |
|---|---|
| .NET 10 / ASP.NET Core 10 | Web API framework |
Asp.Versioning v8 |
API versioning (URL, query string, header) |
Asp.Versioning.Mvc.ApiExplorer |
Swagger integration for versioned docs |
Swashbuckle.AspNetCore v7 |
Swagger / OpenAPI UI |
xUnit v2 |
Unit and integration testing framework |
WebApplicationFactory |
In-process integration test host |
- API versioning in ASP.NET Core β Microsoft Docs
- Asp.Versioning on GitHub
- API Design Best Practices β REST Versioning
This project is licensed under the MIT License.
| Platform | Link |
|---|---|
| π Website | https://codingdroplets.com/ |
| πΊ YouTube | https://www.youtube.com/@CodingDroplets |
| π Patreon | https://www.patreon.com/CodingDroplets |
| β Buy Me a Coffee | https://buymeacoffee.com/codingdroplets |
| π» GitHub | http://github.com/codingdroplets/ |
Want more samples like this? Support us on Patreon or buy us a coffee β β every bit helps keep the content coming!