使用虚拟目录运行 Web API 时无法运行 Swagger

Gio*_*oza 5 c# asp.net swagger owin

我目前正在 .Net Standard 和 ASP.Net 中使用 Swagger 和 Swashbuckle 编写一些 API 文档。

当使用虚拟目录在 LocalIIS 中运行 Web API 时,导航到 ../swagger/ui/index 会导致 404 错误。

在 IISExpress 中运行 Web API 时执行上述步骤会导致 Swagger UI 页面按预期显示。

我在 Google 上查找过类似问题,但大多数问题仍然会导致显示 Swagger UI 页面,但 URL 本身并未解析为正确的地址。当我尝试导航到 Swagger UI Index.html 时,我什至无法做到这一点,因为我得到了 404。

我能找到的最接近我的问题的描述在这里:

https://github.com/domaindrivendev/Swashbuckle/issues/305

我在本文中实施了 StanislavKuzymkiv 建议的修复,但它没有解决我的问题。

作为虚拟目录设置的一部分,是否有任何我可能错过的必须手动复制的 swagger 文件?

我还使用 https 而不是 http(如果这对设置有任何影响)

下面是我的 SwaggerConfig 文件的片段,其中包含 RootUrl“fix”。

        httpConfiguration.EnableSwagger(c =>
        {
            c.MultipleApiVersions(
                (apiDesc, targetApiVersion) => ResolveVersionSupportByRouteConstraint(apiDesc, targetApiVersion),
                (vc) =>
                {
                    vc.Version("v2", "Open API V2");
                    vc.Version("v1", "Open API V1");
                });

            c.RootUrl(req => { var pathBase = req.GetOwinContext().Get<string>("owin.RequestPathBase"); return new Uri(req.RequestUri, pathBase).ToString(); });
            c.Schemes(new[] { "https" });
Run Code Online (Sandbox Code Playgroud)