Configure ASP.NET for the API to work #7

Merged
Wesser merged 17 commits from feat/setting-up-asp into release/v1.0.0 2024-01-26 20:41:01 +03:00
Showing only changes of commit f1ed45af96 - Show all commits

View File

@ -41,8 +41,33 @@ public class Program
policy.AllowAnyOrigin();
});
});
builder.Services.AddApiVersioning(options =>
{
options.DefaultApiVersion = new ApiVersion(1, 0);
options.AssumeDefaultVersionWhenUnspecified = true;
options.ReportApiVersions = true;
options.ApiVersionReader = new UrlSegmentApiVersionReader();
});
builder.Services.AddVersionedApiExplorer(options =>
{
options.GroupNameFormat = "'v'VVV";
options.SubstituteApiVersionInUrl = true;
});
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddSwaggerGen(options =>
{
options.OperationFilter<SwaggerDefaultValues>();
var basePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory);
var xmlPath = Path.Combine(basePath, "docs.xml");
options.IncludeXmlComments(xmlPath);
});
builder.Services.AddTransient<IConfigureOptions<SwaggerGenOptions>, ConfigureSwaggerOptions>();
var app = builder.Build();
@ -50,7 +75,17 @@ public class Program
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
app.UseSwaggerUI(options =>
{
var provider = app.Services.GetService<IApiVersionDescriptionProvider>();
foreach (var description in provider.ApiVersionDescriptions)
{
var url = $"/swagger/{description.GroupName}/swagger.json";
var name = description.GroupName.ToUpperInvariant();
options.SwaggerEndpoint(url, name);
}
});
}
app.UseHttpsRedirection();