Use this file to discover all available pages before exploring further.
The Intent.AspNetCore.Versioning module applies versioning to your ASP.NET Core services, enabling you to maintain multiple versions of your API simultaneously while evolving your application.
API versioning is essential for maintaining backward compatibility while introducing new features and changes. This module integrates Microsoft’s ASP.NET Core API Versioning library to provide a robust versioning strategy for your Web APIs.
public class ApiVersionSwaggerGenOptions : IConfigureOptions<SwaggerGenOptions>{ private readonly IApiVersionDescriptionProvider _provider; public ApiVersionSwaggerGenOptions(IApiVersionDescriptionProvider provider) { _provider = provider; } public void Configure(SwaggerGenOptions options) { foreach (var description in _provider.ApiVersionDescriptions) { options.SwaggerDoc( description.GroupName, new OpenApiInfo { Title = $"My API {description.ApiVersion}", Version = description.ApiVersion.ToString(), Description = description.IsDeprecated ? "This API version has been deprecated." : "Current API version" }); } }}
[ApiController][ApiVersion("1.0")][Route("api/v{version:apiVersion}/products")]public class ProductsV1Controller : ControllerBase{ [HttpGet] public ActionResult<List<ProductDtoV1>> GetAll() { // Version 1 implementation }}[ApiController][ApiVersion("2.0")][Route("api/v{version:apiVersion}/products")]public class ProductsV2Controller : ControllerBase{ [HttpGet] public ActionResult<List<ProductDtoV2>> GetAll() { // Version 2 implementation with new features }}
[ApiController][ApiVersion("1.0")][ApiVersion("2.0")][Route("api/products")]public class ProductsController : ControllerBase{ [HttpGet] [MapToApiVersion("1.0")] public ActionResult<List<ProductDtoV1>> GetAllV1() { // Version 1 } [HttpGet] [MapToApiVersion("2.0")] public ActionResult<List<ProductDtoV2>> GetAllV2() { // Version 2 }}
Client requests:
GET /api/products?api-version=1.0GET /api/products?api-version=2.0
[ApiController][ApiVersion("1.0")][ApiVersion("2.0")][Route("api/products")]public class ProductsController : ControllerBase{ // Same implementation as query string versioning}
Client requests:
GET /api/productsX-Api-Version: 1.0GET /api/productsX-Api-Version: 2.0
[ApiVersion("1.0")][ApiController][Route("api/v{version:apiVersion}/orders")]public class OrdersController : ControllerBase{ // All endpoints are v1.0}
[ApiVersion("1.0")][ApiVersion("2.0")][ApiController][Route("api/v{version:apiVersion}/orders")]public class OrdersController : ControllerBase{ [HttpGet] public ActionResult<List<OrderDto>> GetAll() { // Shared across versions } [HttpGet("{id}")] [MapToApiVersion("1.0")] public ActionResult<OrderDtoV1> GetByIdV1([FromRoute] Guid id) { // Version 1 specific } [HttpGet("{id}")] [MapToApiVersion("2.0")] public ActionResult<OrderDtoV2> GetByIdV2([FromRoute] Guid id) { // Version 2 with additional data }}
[ApiVersionNeutral][ApiController][Route("api/health")]public class HealthController : ControllerBase{ [HttpGet] public ActionResult<HealthStatus> Check() { // Available in all versions }}
[ApiVersion("1.0", Deprecated = true)][ApiVersion("2.0")][ApiController][Route("api/v{version:apiVersion}/products")]public class ProductsController : ControllerBase{ // Version 1.0 is deprecated but still functional}
// V1 - Originalpublic class ProductDtoV1{ public Guid Id { get; set; } public string Name { get; set; } public decimal Price { get; set; }}// V2 - Breaking change: Price split into componentspublic class ProductDtoV2{ public Guid Id { get; set; } public string Name { get; set; } public decimal BasePrice { get; set; } public decimal TaxAmount { get; set; } public decimal TotalPrice { get; set; }}
Add new properties without breaking existing clients:
// V1 and V2 use same DTOpublic class ProductDto{ public Guid Id { get; set; } public string Name { get; set; } public decimal Price { get; set; } // New in V2, ignored by V1 clients public string? Category { get; set; } public List<string>? Tags { get; set; }}
GET /api/v99/productsHTTP/1.1 400 Bad Request{ "error": { "code": "UnsupportedApiVersion", "message": "The HTTP resource that matches the request URI 'http://localhost/api/v99/products' does not support the API version '99'.", "innerError": null }}
[Fact]public async Task GetProducts_V1_ReturnsV1Format(){ // Arrange var controller = new ProductsV1Controller(_service); // Act var result = await controller.GetAll(); // Assert var okResult = Assert.IsType<ActionResult<List<ProductDtoV1>>>(result); Assert.NotNull(okResult.Value);}[Fact]public async Task GetProducts_V2_ReturnsV2Format(){ // Arrange var controller = new ProductsV2Controller(_service); // Act var result = await controller.GetAll(); // Assert var okResult = Assert.IsType<ActionResult<List<ProductDtoV2>>>(result); Assert.NotNull(okResult.Value);}