您可以将 swagger 文档放在根目录中。
然后映射与设置为 swagger 端点相同的路由。
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("https://localhost:7040/mypath/swaggerDoc", "customSwagger");
});
app.MapGet("/mypath/swaggerDoc", async context =>
{
await context.Response.WriteAsync(await File.ReadAllTextAsync(Path.Combine(Directory.GetCurrentDirectory(), "swagger.json")));
});