这是我在 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 应该有一个用于版本选择的下拉菜单。任何人都可以帮助我吗?