更新Program.cs
添加两个版本的SwaggerDoc
//Swagger
builder.Services.AddEndpointsApiExplorer(); //generates description for all endpoints
builder.Services.AddSwaggerGen(options =>
{
options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, "api.xml"));
options.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo() { Title = "Cities Web API", Version = "1.0" });
options.SwaggerDoc("v2", new Microsoft.OpenApi.Models.OpenApiInfo() { Title = "Cities Web API", Version = "2.0" });
}); //generates OpenAPI specification
var app = builder.Build();
更新UseSwaggerUI
app.UseSwagger(); //created endpoint for swagger.json
app.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("/swagger/v1/swagger.json", "1.0");
options.SwaggerEndpoint("/swagger/v2/swagger.json", "2.0");
}); //creates swagger UI for testing all Web Api endpoints/action methods
添加AddVersionedApiExplorer
builder.Services.AddVersionedApiExplorer(options =>
{
options.GroupNameFormat = "'v'VVV";
options.SubstituteApiVersionInUrl = true;
});
var app = builder.Build();
结果
程序运行后有两个版本的Swagger 1.0和2.0
Gitee获取源码:
https://gitee.com/huang_jianhua0101/asp.-net-core-8.git
标签:03,Web,Versions,builder,API,app,swagger,options From: https://blog.csdn.net/KevinHuang2088/article/details/139843851