Advertisement
Advanced ASP.NET Core, API Versioning

How do you implement API versioning for minimal API endpoints, since they don't use controller attributes?

Short answer

Asp.Versioning.Http provides a NewVersionedApi() builder extension and .HasApiVersion()/.MapToApiVersion() methods for grouping and versioning minimal API endpoint groups, mirroring the controller-based attribute approach but using a fluent, code-based API appropriate for the…

Save ASP.NET Core mock interview
Advertisement
AModel answer
Asp.Versioning.Http provides a NewVersionedApi() builder extension and .HasApiVersion()/.MapToApiVersion() methods for grouping and versioning minimal API endpoint groups, mirroring the controller-based attribute approach but using a fluent, code-based API appropriate for the minimal API style.
var versionSet = app.NewApiVersionSet()
    .HasApiVersion(new ApiVersion(1, 0))
    .HasApiVersion(new ApiVersion(2, 0))
    .ReportApiVersions()
    .Build();

var group = app.MapGroup("api/v{version:apiVersion}/products").WithApiVersionSet(versionSet);

group.MapGet("/", () => Ok(v1Products)).HasApiVersion(1.0);
group.MapGet("/", () => Ok(v2Products)).HasApiVersion(2.0);
Real-world example
A minimal-API-based microservice adopts the same URL-segment versioning strategy as its controller-based sibling services, using NewApiVersionSet and MapGroup to keep the versioning approach consistent across the organization's differing API styles.
Likely follow-ups
  • How does WithApiVersionSet differ from applying versioning per individual endpoint?
  • Can minimal APIs and controllers share the same versioning configuration in one app?
Related topics