Skip to content

Instantly share code, notes, and snippets.

@dj-nitehawk
Created July 11, 2023 11:28
Show Gist options
  • Save dj-nitehawk/fbefbcb6273d372e5e5913accb18ab76 to your computer and use it in GitHub Desktop.
Save dj-nitehawk/fbefbcb6273d372e5e5913accb18ab76 to your computer and use it in GitHub Desktop.
Asp.Versioning.Http Example
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");
}
}
@VictorioBerra
Copy link

I think HasApiVersion(...) should be HasApiVersion(new(...))

@dj-nitehawk
Copy link
Author

@VictorioBerra it's an extension method overload coming from using Asp.Versioning.Conventions;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment