首页 > 解决方案 > 如何在使用 .com 部署在 IIS 中的 .Net Framework 4.7 项目中显示 swagger/ui/index 页面

问题描述

我有一个在 .NET Framework 4.7 中运行的 web api 项目。我添加了 Reference Swashbuckle.Core 来显示 swagger ui。我还更新了 .csproj 以添加文档 .xml 文件。当我使用 url http://localhost:55677/swagger/ui/index运行我的应用程序时,它在我的开发机器上工作。但是当我使用 Visual Studio 发布应用程序时,它显示为“404 - 找不到文件或目录”。这可能是因为 UI 页面 Swaggerui 在已发布的 bin 文件夹中不可用。发布的 url 看起来像“ https://xxx.xxx.xxx.com ”。但是加载后我给了“ https://xxx.xxx.xxx.com/swagger/ui/index“显示招摇的用户界面。但它显示404错误。我没有使用任何.net核心项目。只是.net框架。

有人请帮我解决这个问题。

在查看了几个站点后,我在 SwaggerConfig.cs 文件中添加了这些代码部分。

GlobalConfiguration.Configuration
            .EnableSwagger(c =>
                {
                    c.SingleApiVersion("v1", "App Name");
                    // Set the comments path for the Swagger JSON and UI.
                   var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                    var xmlPath = Path.Combine(AppContext.BaseDirectory + "bin\\", xmlFile);
                    c.IncludeXmlComments(xmlPath);                        
                    c.ApiKey(ConfigurationManager.AppSettings.Get("apikey"))
                    .Description("API Key Authentication")
                    .Name("key-name")
                    .In("header");
                    c.RootUrl(req =>
                        req.RequestUri.GetLeftPart(UriPartial.Authority) +
                        req.GetRequestContext().VirtualPathRoot.TrimEnd('/'));
                })
            .EnableSwaggerUi(c =>
                {
                    c.EnableApiKeySupport("key-name", "header");
                });

RootUrl 是我从网站上找到的重定向到 SwaggerUI 页面的那个。但它不起作用。

标签: c#asp.netasp.net-web-apiswagger-uiasp.net-web-api-routing

解决方案


推荐阅读