简体   繁体   English

Swagger UI 中的 API 版本控制

[英]API versioning in Swagger UI

I'm looking into .net core 6, API versioning and trying to show two versions "v1" and "v2" in swagger UI but only "v1" is showing though no error is appearing.我正在研究 .net 核心 6、API 版本控制,并试图在 swagger UI 中显示两个版本“v1”和“v2”,但仅显示“v1”。

Here is my program.cs code这是我的program.cs代码

   builder.Services.AddApiVersioning(setup =>
    {
        setup.DefaultApiVersion = new ApiVersion(1,0);
        setup.AssumeDefaultVersionWhenUnspecified = true;
        setup.ReportApiVersions = true;
    });

    builder.Services.AddVersionedApiExplorer(setup =>
    {
        setup.GroupNameFormat = "'v'VVV";
        setup.SubstituteApiVersionInUrl = true;
    });

    builder.Services.AddSwaggerGen(options =>
    {
        
        options.SwaggerDoc("v1", new OpenApiInfo
        {
            Version = "v1",
            Title = "API",
            Description = "An API in dotnet core 6",

        });
        var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
        options.IncludeXmlComments(Path.Combine($@"{AppContext.BaseDirectory}", xmlFilename));
    });

    if (app.Environment.IsDevelopment())
    {
        app.UseSwagger();
        app.UseSwaggerUI();
    }

v1 controller have this action method v1 controller 有这个动作方法

    [MapToApiVersion("1.0")]
    [HttpGet]
    [Route("GetLast12MonthBalances")]
    public async Task<ApiResponse> GetLast12MonthBalances()
    {

v2 controller have this action method v2 controller 有这个动作方法

   [MapToApiVersion("2.0")]
   [HttpGet,Route("GetLast12MonthBalances")]
   public async Task<ApiResponse> GetLast12MonthBalances()
   {

Any idea what is missing?知道缺少什么吗?

You need to configure SwaggerGenOptions using options pattern.您需要使用选项模式配置 SwaggerGenOptions。

1st- Create a class as follows: 1st- 创建一个 class 如下:

namespace MyApi
{
    public class ConfigureSwaggerOptions
        : IConfigureNamedOptions<SwaggerGenOptions>
    {
        private readonly IApiVersionDescriptionProvider provider;

        public ConfigureSwaggerOptions(
            IApiVersionDescriptionProvider provider)
        {
            this.provider = provider;
        }

        public void Configure(SwaggerGenOptions options)
        {
            // add swagger document for every API version discovered
            foreach (var description in provider.ApiVersionDescriptions)
            {
                options.SwaggerDoc(
                    description.GroupName, 
                    CreateVersionInfo(description));
            }
        }

        public void Configure(string name, SwaggerGenOptions options)
        {
            Configure(options);
        }

        private OpenApiInfo CreateVersionInfo(
                ApiVersionDescription description)
        {
            var info = new OpenApiInfo()
            {
                Title = "My API",
                Version = description.ApiVersion.ToString()
            };

            if (description.IsDeprecated)
            {
                info.Description += " deprecated API.";
            }

            return info;
        }
    }
}

2-wire up the options with the service collection将选项与服务集合连接起来

public void ConfigureServices(IServiceCollection services)
{
    services.AddScoped<ITestsRepository, TestssRepository>();
    services.AddControllers();

    services.AddApiVersioning(setup =>
    {
        setup.DefaultApiVersion = new ApiVersion(1, 0);
        setup.AssumeDefaultVersionWhenUnspecified = true;
        setup.ReportApiVersions = true;
    });

    services.AddVersionedApiExplorer(setup =>
    {
        setup.GroupNameFormat = "'v'VVV";
        setup.SubstituteApiVersionInUrl = true;
    });

    services.AddSwaggerGen();
    services.ConfigureOptions<ConfigureSwaggerOptions>();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swagger UI - 使用 Microsoft.AspNet.WebApi.Versioning 时的 Web API 版本控制 - Swagger UI - Web API versioning when using Microsoft.AspNet.WebApi.Versioning 使用api版本控制的Autofac和swagger给出了例外 - Autofac and swagger with api versioning gives exception 带有醒目的版本控制的Web API,未找到ResolveVersionSupportByRouteConstraint - web api with swagger versioning, ResolveVersionSupportByRouteConstraint not found 具有多个端点的 Swagger 文档中的 RESTful api 版本控制和分组 - RESTful api versioning and grouping in doc from Swagger with multiple endpoints .Net Core 3.1 swagger API 版本冲突命名空间 url - .Net Core 3.1 swagger API versioning conflicting namespaces url 无法加载 swagger API 页面,缺少 swagger-ui.css - Unable to Load swagger API page, missing swagger-ui.css 将身份验证添加到/ swagger / ui / index页面 - Swagger | Web API | Swashbuckle - Add Authentication to /swagger/ui/index page - Swagger | Web API | Swashbuckle swagger-ui中默认api版本值 - api version value by default in swagger-ui Swagger在API UI屏幕上显示错误部分 - Swagger showing error section on API UI Screen 在 IIS 上部署 API 时,Swagger UI 不显示 - Swagger UI not displaying when deploying API on IIS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM