1

这是我在 startup.cs 中的配置

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


        // Register the Swagger services
        services.AddSwaggerDocument(config =>
        {
            config.DocumentName = "v1";
            config.ApiGroupNames = new[] { "1" };
            config.GenerateEnumMappingDescription = true;
            config.AddSecurity("bearer", Enumerable.Empty<string>(), new OpenApiSecurityScheme
            {
                Type = OpenApiSecuritySchemeType.OAuth2,
                Description = "Get access to data while protecting your account credentials. OAuth2 is also a safer and more secure way to give you access.",
                Flow = OpenApiOAuth2Flow.Implicit,
                Flows = new OpenApiOAuthFlows()
                {
                    Implicit = new OpenApiOAuthFlow()
                    {

                        Scopes = new Dictionary<string, string>
                        {
                            {Configuration.GetValue<string>("IdentityServer:ClientId"), Configuration.GetValue<string>("IdentityServer:ClientId")},
                            {Configuration.GetValue<string>("IdentityServer:BankClientId"), Configuration.GetValue<string>("IdentityServer:BankClientId")}

                        },
                        TokenUrl = Configuration.GetValue<string>("IdentityServer:Authority") + "/connect/token",
                        AuthorizationUrl = Configuration.GetValue<string>("IdentityServer:Authority") + "/connect/authorize",

                    },
                }
            });
            config.OperationProcessors.Add(new AspNetCoreOperationSecurityScopeProcessor("bearer"));
            config.PostProcess = document =>
            {
                document.Info.Version = Configuration.GetValue<String>("SwaggerDocument:Version");
                document.Info.Title = Configuration.GetValue<String>("SwaggerDocument:Title");
                document.Info.Description = Configuration.GetValue<String>("SwaggerDocument:Description");
            };
        })
        .AddSwaggerDocument(document =>
            {
                document.DocumentName = "v2";
                document.ApiGroupNames = new[] { "2" };
                document.GenerateEnumMappingDescription = true;
                document.AddSecurity("bearer", Enumerable.Empty<string>(), new OpenApiSecurityScheme
                {
                    Type = OpenApiSecuritySchemeType.OAuth2,
                    Description = "Get access to data while protecting your account credentials. OAuth2 is also a safer and more secure way to give you access.",
                    Flow = OpenApiOAuth2Flow.Implicit,
                    Flows = new OpenApiOAuthFlows()
                    {
                        Implicit = new OpenApiOAuthFlow()
                        {

                            Scopes = new Dictionary<string, string>
                        {
                            {Configuration.GetValue<string>("IdentityServer:ClientId"), Configuration.GetValue<string>("IdentityServer:ClientId")},
                            {Configuration.GetValue<string>("IdentityServer:BankClientId"), Configuration.GetValue<string>("IdentityServer:BankClientId")}

                        },
                            TokenUrl = Configuration.GetValue<string>("IdentityServer:Authority") + "/connect/token",
                            AuthorizationUrl = Configuration.GetValue<string>("IdentityServer:Authority") + "/connect/authorize",

                        },
                    }
                });
                document.OperationProcessors.Add(new AspNetCoreOperationSecurityScopeProcessor("bearer"));
                document.PostProcess = document =>
                {
                    document.Info.Version = "v2";
                    document.Info.Title = Configuration.GetValue<String>("SwaggerDocument:Title");
                    document.Info.Description = Configuration.GetValue<String>("SwaggerDocument:Description");
                };
            });

        app.UseOpenApi();
        
        //Redoc
        app.UseReDoc(options =>
        {
            options.Path = Configuration.GetValue<String>("Redoc:Path");
            options.DocumentPath = Configuration.GetValue<String>("Redoc:DocumentPath");
        });

API 版本显示为招摇。下面是图片

在此处输入图像描述

但同样的事情并没有发生在 REDOC 上。下面是图片

在此处输入图像描述

如果我将 url 从 https://localhost:44311/redoc/index.html?url=/swagger/v1/swagger.json 更改为 https://localhost:44311/redoc/index.html?url=/swagger/ v2/swagger.json 只需将 v1 更改为 v2 即可获得 v2 的 API。但我希望 REDOC UI 应该有一个用于版本选择的下拉菜单。任何人都可以帮助我吗?

4

2 回答 2

1

这在.net 5中对我有用:

大摇大摆->/docs

redoc -> /docs-v1, /docs-v2, 等等。

 public void Configure(IApplicationBuilder app, IApiVersionDescriptionProvider provider){

        ...

        app.UseSwagger(options => { options.RouteTemplate = "docs/{documentName}/docs.json"; });
        app.UseSwaggerUI(options =>
        {
            options.RoutePrefix = "docs";
            foreach (var description in provider.ApiVersionDescriptions)
            {
                options.SwaggerEndpoint($"/docs/{description.GroupName}/docs.json", description.GroupName.ToUpperInvariant());
            }
        });

        foreach (var description in provider.ApiVersionDescriptions)
        {
            app.UseReDoc(options =>
            {
                options.DocumentTitle = $"API Documentation {description.GroupName}";
                options.SpecUrl = $"/docs/{description.GroupName}/docs.json";
                options.RoutePrefix = $"docs-{description.GroupName}";
            });
        }

}
于 2021-08-31T09:19:29.050 回答
1

您不能拥有 UI 选择器,但您绝对可以更改不同版本的 URL 上的行为。这意味着您可以为每个版本设置不同的 URL。当您配置它时,简单的方法是定义如下 URL。

请注意,我假设您了解版本控制以使其正常工作,并且一切都在使用 Swagger 版本控制。所以我所拥有的是我在我的Configure()方法上获得了一个IApiVersionDescriptionProvider参数。然后,它允许我迭代不同的版本并创建不同的 ReDoc(s)。

public void Configure(IApplicationBuilder app, IApiVersionDescriptionProvider provider){
 
 app.UseSwagger();

 // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), 
 // specifying the Swagger JSON endpoint.
 app.UseSwaggerUI(c =>
 {
     foreach (var description in provider.ApiVersionDescriptions)
      {
                c.SwaggerEndpoint($"/swagger/{description.GroupName}/swagger.json",
                    $"Your API {description.GroupName.ToUpperInvariant()}");
      }
      c.OAuthUsePkce();
      c.OAuthClientId("something");
      c.OAuthAppName("Something");
 });

 //Use ReDoc
 app.UseReDoc(c =>
 {
   foreach (var description in provider.ApiVersionDescriptions)
    {
      c.DocumentTitle = $"MY API Documentation {description.GroupName.ToUpperInvariant()}";
      c.RoutePrefix = $"{documentation/{description.GroupName}";
      c.SpecUrl = $"{/swagger/{description.GroupName}/swagger.json";
    }
 });
}
于 2021-05-12T20:48:35.060 回答