引入Nuget包
dotnet add package Swashbuckle.AspNetCore --version 5.5.1
dotnet add package Microsoft.AspNetCore.Mvc.Versioning --version 4.1.1
dotnet add package Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer --version 4.1.1
ConfigureServices
// API版本控制
services.AddApiVersioning(o =>
{
o.AssumeDefaultVersionWhenUnspecified = true;
o.DefaultApiVersion = new ApiVersion(1, 0);
}).AddVersionedApiExplorer(o =>
{
o.GroupNameFormat = "'v'V";
});
services.AddSwaggerGen(c =>
{
var providr = services.BuildServiceProvider().GetRequiredService<IApiVersionDescriptionProvider>();
foreach (var description in providr.ApiVersionDescriptions)
{
c.SwaggerDoc(description.GroupName, new OpenApiInfo
{
Title = $"Api接口 v{description.ApiVersion}",
Version = description