swagger-ui 中默认的 api 版本值

Mik*_*iky 7 c# swagger-ui asp.net-core

我已经在我们的asp.core wep-api项目中配置了 swagger并且它工作得非常好。现在我正在研究当swagger-ui出现时的解决方案,如下所示

https://imgur.com/a/K7QTKCu

api 版本部分应根据代码端的配置自动填充。

        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new Info
            {
                Version = "v1",
                Title = "My API",
                Contact = new Contact
                {
                    Name = "My Api",
                    Url = "https://109.com/"
                }
            });
            var security = new Dictionary<string, IEnumerable<string>>
            {
                {"Bearer", new string[] { }},
            };
            c.AddSecurityDefinition("Bearer", new ApiKeyScheme
            {
                Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
                Name = "Authorization",
                In = "header",
                Type = "apiKey"
            });
            c.AddSecurityRequirement(security);
        });
Run Code Online (Sandbox Code Playgroud)

use*_*994 9

您需要安装Microsoft.AspNetCore.Mvc.VersioningMicrosoft.AspNetCore.Mvc.Versioning.ApiExplorer打包以在 Swagger 中启用 API 版本控制。

您可以在此处查看其他详细信息。

ConfigureServices方法中将版本控制方案定义为

 services.AddVersionedApiExplorer(o =>
 {
      o.GroupNameFormat = "'v'VVV";
      o.SubstituteApiVersionInUrl = true;
 });
 services.AddApiVersioning(config =>
 {
     config.DefaultApiVersion = new ApiVersion(1, 0);
     config.AssumeDefaultVersionWhenUnspecified = true;
     config.ReportApiVersions = true;
 });
Run Code Online (Sandbox Code Playgroud)