Created
July 11, 2023 11:28
-
-
Save dj-nitehawk/fbefbcb6273d372e5e5913accb18ab76 to your computer and use it in GitHub Desktop.
Asp.Versioning.Http Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Asp.Versioning; | |
using Asp.Versioning.Conventions; | |
using FastEndpoints; | |
using FastEndpoints.AspVersioning; | |
using FastEndpoints.Swagger; | |
VersionSets.CreateApi(">>Orders<<", v => v | |
.HasApiVersion(1.0) | |
.HasApiVersion(2.0)); | |
VersionSets.CreateApi(">>Inventory<<", v => | |
{ | |
v.HasApiVersion(1.0); | |
v.HasApiVersion(1.1); | |
}); | |
var builder = WebApplication.CreateBuilder(); | |
builder.Services | |
.AddFastEndpoints() | |
.AddVersioning(o => | |
{ | |
o.DefaultApiVersion = new(1.0); | |
o.AssumeDefaultVersionWhenUnspecified = true; | |
o.ApiVersionReader = new HeaderApiVersionReader("X-Api-Version"); | |
}) | |
.SwaggerDocument(o => | |
{ | |
o.DocumentSettings = x => | |
{ | |
x.DocumentName = "version one"; | |
x.ApiVersion(new(1.0)); | |
}; | |
o.AutoTagPathSegmentIndex = 0; | |
}) | |
.SwaggerDocument(o => | |
{ | |
o.DocumentSettings = x => | |
{ | |
x.DocumentName = "version one point one"; | |
x.ApiVersion(new(1.1)); | |
}; | |
o.AutoTagPathSegmentIndex = 0; | |
}) | |
.SwaggerDocument(o => | |
{ | |
o.DocumentSettings = x => | |
{ | |
x.DocumentName = "version two"; | |
x.ApiVersion(new(2.0)); | |
}; | |
o.AutoTagPathSegmentIndex = 0; | |
}); | |
var app = builder.Build(); | |
app.UseAuthorization() | |
.UseFastEndpoints() | |
.UseSwaggerGen(); | |
app.Run(); | |
public class GetInvoices_v1 : EndpointWithoutRequest | |
{ | |
public override void Configure() | |
{ | |
Get("invoices"); | |
AllowAnonymous(); | |
Options(x => x | |
.WithVersionSet(">>Orders<<") | |
.MapToApiVersion(1.0)); | |
} | |
public override async Task HandleAsync(CancellationToken c) | |
{ | |
await SendAsync("v1 - orders"); | |
} | |
} | |
public class GetInvoices_v2 : EndpointWithoutRequest | |
{ | |
public override void Configure() | |
{ | |
Get("invoices"); | |
AllowAnonymous(); | |
Options(x => x | |
.WithVersionSet(">>Orders<<") | |
.MapToApiVersion(2.0)); | |
} | |
public override async Task HandleAsync(CancellationToken c) | |
{ | |
await SendAsync("v2 - orders"); | |
} | |
} | |
public class GetStockItems_v1 : EndpointWithoutRequest | |
{ | |
public override void Configure() | |
{ | |
Get("stock-items"); | |
AllowAnonymous(); | |
Options(x => x | |
.WithVersionSet(">>Inventory<<") | |
.MapToApiVersion(1.0)); | |
} | |
public override async Task HandleAsync(CancellationToken c) | |
{ | |
await SendAsync("v1 - inventory"); | |
} | |
} | |
public class GetStockItems_v1_1 : EndpointWithoutRequest | |
{ | |
public override void Configure() | |
{ | |
Get("stock-items"); | |
AllowAnonymous(); | |
Options(x => x | |
.WithVersionSet(">>Inventory<<") | |
.MapToApiVersion(1.1)); | |
} | |
public override async Task HandleAsync(CancellationToken c) | |
{ | |
await SendAsync("v1.1 - inventory"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think
HasApiVersion(...)
should beHasApiVersion(new(...))