首页 > 解决方案 > swagger-ui 中默认的 api 版本值

问题描述

我已经在我们的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);
        });

标签: c#asp.net-coreswagger-ui

解决方案


您需要安装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;
 });

推荐阅读