问题描述
这是我在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",new OpenApiSecurityScheme
{
Type = OpenApiSecuritySchemeType.OAuth2,Flows = new OpenApiOAuthFlows()
{
Implicit = new OpenApiOAuthFlow()
{
Scopes = new Dictionary<string,}
});
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");
});
但是REDOC并没有发生同样的事情。下面是图片
如果我将网址从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应该有一个用于版本选择的下拉列表。有人可以帮助我吗?
解决方法
您不能拥有 UI 选择器,但您绝对可以更改不同版本的 URL 上的行为。这意味着您可以为每个版本使用不同的 URL。配置它时,最简单的方法是定义如下 URL。
请注意,我将假设您了解版本控制以使其正常工作,并且所有这些都在使用 Swagger 版本控制。所以我所拥有的是在我的 Configure() 方法中获得了一个 IApiVersionDescriptionProvider 参数。然后,它允许我迭代不同的版本并创建不同的 ReDoc。
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";
}
});
}