c# - Swagger 'swagger.json' 加载,但在 AspNet 项目中的 swagger UI '{localhost}/swagger' 上出现 404 错误

标签 c# asp.net-core swagger swagger-ui swashbuckle

正在使用 AspNetCore 为使用 IIS 托管的 Web 应用程序设置 swagger。 .json 页面加载并且似乎可以很好地接触所有 API,但是当导航到 {localhost}/swagger 以查看 UI 页面时,我收到了 404 错误。我在 Startup.cs 中有以下代码:

//Configure Services


     services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info { Title = "API ", Version = "v1" });
                c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
            });

            }


//Configure
            app.UseSwagger();
            app.UseStaticFiles();

            app.UseDeveloperExceptionPage();

            // Enable middleware to serve generated Swagger as a JSON endpoint.

            // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
            // specifying the Swagger JSON endpoint.
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("./swagger/v1/swagger.json", "My API V1");
                //c.RoutePrefix = string.Empty;
            });

            app.UseMvc();
        }


有没有人看到任何潜在的问题?或者在故障排除方面有什么建议?已经在这方面工作了一段时间,希望有一双新的眼睛可以看到我没有看到的东西。

最佳答案

来自 MS Docs ,如果您希望在 Web 应用程序的根目录提供 swagger UI,则将 routePrefix 设置为空字符串。

To serve the Swagger UI at the app's root (http://localhost:/), set the RoutePrefix property to an empty string



默认为“/swagger”;看起来你正在覆盖这个,这就是为什么 UI 没有显示在“/swagger”。只需点击“localhost:”,UI 就会出现。

删除 routePrefix 的分配,它应该像您期望的那样显示在“/swagger”中。

关于c# - Swagger 'swagger.json' 加载,但在 AspNet 项目中的 swagger UI '{localhost}/swagger' 上出现 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57043344/

相关文章:

c# - 如何在 ASP.NET 5 中序列化 Rsa 签名凭据?

c# - POST 变量上每个循环的 ASHX C#

c# - 更新拥有的实体 EF Core 5

servicestack - Swagger (ServiceStack) 中未准确记录通用类型的响应对象

python - 使用 Flask 和 Swagger 公开 API 文档

c# - 如何获取 expando 对象的调用成员名称?

c# - 使用 dll 方法时出现 FileNotFoundException

c# - 通过迁移动态创建数据库

javascript - Angular 应用程序中的 Google Chrome "Refused to load the script"错误

python - Swagger - Python 客户端 - 禁用 SSL 验证