首页 > 解决方案 > 在 IIS 上托管 NetCore 3.1 Web Api 时找不到 openapi.json

问题描述

我使用带有以下命令的 openapi 生成器为 ASP.NET Core 3.1 生成了我的服务器存根:

npx @openapitools/openapi-generator-cli generate -i myapi.json -g aspnetcore -o C:\myapi --additional-properties aspnetCoreVersion=3.1

在 Visual Studio 2019 中打开该项目并运行它后,我会得到一个招摇页面,我可以在其中尝试不同的端点。到目前为止还可以。

但是:在我们的 IIS 服务器上发布它并访问正确的 URL 后,swagger 页面正在加载错误:

未找到:/swagger/1.0.0/openapi.json

IIS 上的 Swagger 错误

可以到达端点本身(GET 请求)。

startup.cs 中的 Swagger Endpoint 代码是由 open-api 生成器生成的默认代码:

app.UseSwagger(c =>
            {
                c.RouteTemplate = "swagger/{documentName}/openapi.json";
            })
            .UseSwaggerUI(c =>
            {
                //TODO: Either use the SwaggerGen generated Swagger contract (generated from C# classes)
                c.SwaggerEndpoint("/swagger/1.0.0/openapi.json", "Swagger WP RUH Delivery");

                //TODO: Or alternatively use the original Swagger contract that's included in the static files
                // c.SwaggerEndpoint("/openapi-original.json", "Swagger WP RUH Delivery Original");
            });

为什么这个 Swagger 页面在我的机器上工作,但在 IIS 上却出错了?我发现了一些关于在 startup.cs 中更改 c.SwaggerEndpoint 的提示,但没有任何帮助。

标签: iis.net-core-3.1openapi-generator

解决方案


尝试/swagger/从 URL 路径中删除:

c.SwaggerEndpoint("1.0.0/openapi.json", "Swagger WP RUH Delivery")

推荐阅读