【问题标题】:Swagger 'swagger.json' loads, but 404 error on swagger UI '{localhost}/swagger' in AspNet projectSwagger 'swagger.json' 加载,但在 AspNet 项目中的 swagger UI '{localhost}/swagger' 上出现 404 错误
【发布时间】:2019-11-24 08:51:02
【问题描述】:

正在为使用 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();
        }

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

【问题讨论】:

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


    【解决方案1】:

    MS Docs 开始,如果您希望在 Web 应用程序的根目录中提供 Swagger UI,请将 routePrefix 设置为空字符串。

    要在应用的根目录 (http://localhost:/) 提供 Swagger UI,请将 RoutePrefix 属性设置为空字符串

    默认为“/swagger”;看起来您正在覆盖它,这就是 UI 未显示在“/swagger”的原因。只需点击“localhost:”,用户界面就会出现。

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

    【讨论】:

    • jbsmith - 我注释掉了 RoutePrefix 行。但是,现在我看到的不是 chrome 404 页面,而是 IIS 404 页面(导航到 {localhost}/swagger 时)。想法?
    • 为我工作。谢谢。
    • 它有效。谢谢。
    【解决方案2】:

    尝试使用 SwaggerEndpoint 的相对路径:

     app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("../swagger/v1/swagger.json", "My API V1");
            });
    

    然后导航到 {App Url}/swagger。

    参考https://github.com/domaindrivendev/Swashbuckle/issues/971

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-13
      • 2023-03-23
      • 2019-12-23
      • 2018-06-11
      • 2020-06-06
      • 2019-07-29
      • 1970-01-01
      相关资源
      最近更新 更多