我尝试将 swagger 文档拆分为同一版本下的不同部分,以便我可以在端点中访问它们,如下所示:
app.UseSwagger(s =>
{
s.SerializeAsV2 = true;
})
.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger-file1.json", "File1 API V1");
c.SwaggerEndpoint("/swagger/v1/swagger-file2.json", "File2 API V1");
c.SwaggerEndpoint("/swagger/v1/swagger-file3.json", "File3 API V1");
})
不幸的是,我找不到自定义 swagger-file1.json 部分的方法。
我尝试到处研究,但找不到任何可以帮助解决这个特定问题的东西
您可以尝试使用“MapGet”来路由 swagger 文件。例如,我们在根目录中有文件:
然后你可以这样映射:
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI(c =>
{
//c.SwaggerEndpoint("/swagger/swagger.json", "customSwagger");
c.SwaggerEndpoint("/swagger/v1/swagger-file1.json", "File1 API V1");
c.SwaggerEndpoint("/swagger/v1/swagger-file2.json", "File2 API V1");
c.SwaggerEndpoint("/swagger/v1/swagger-file3.json", "File3 API V1");
});
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.MapGet("/swagger/v1/{fileName}", async context =>
{
string file= context.Request.RouteValues["fileName"].ToString();
await context.Response.WriteAsync(await File.ReadAllTextAsync(Path.Combine(Directory.GetCurrentDirectory(), file)));
});