简体   繁体   English

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

[英]Swagger 'swagger.json' loads, but 404 error on swagger UI '{localhost}/swagger' in AspNet project

Working on setting up swagger for a web application hosted with IIS using AspNetCore.使用 AspNetCore 为 IIS 托管的 web 应用程序设置 swagger。 The.json page loads and seems to be touching all the API just fine, however when navigating to {localhost}/swagger to view the UI page I receive a 404 error. .json 页面加载并且似乎触及所有 API 就好了,但是当导航到 {localhost}/swagger 查看 UI 页面时,我收到 404 错误。 I have the following code in Startup.cs:我在 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();
        }

Does anyone see any potential issues with this?有没有人看到任何潜在的问题? Or have any suggestions in terms of troubleshooting?或者在故障排除方面有什么建议? Have been working on this for a while now, hopefully a fresh set of eyes can see something I'm not.已经为此工作了一段时间,希望有新的眼睛能看到我没有看到的东西。

From the MS Docs , you set the routePrefix to an empty string if you want the swagger UI to be served at the root of your web app.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要在应用程序的根 ( http://localhost :/) 处提供 Swagger UI,请将 RoutePrefix 属性设置为空字符串

The default is "/swagger";默认为“/swagger”; it looks like you're overriding this, which is why the UI is not showing at "/swagger."看起来你正在覆盖这个,这就是为什么 UI 没有显示在“/swagger”。 Just hit "localhost:" and the UI should show up.只需点击“localhost:”,UI 就会出现。

Remove that assignment of the routePrefix and it should show up at "/swagger" like you expect.删除 routePrefix 的分配,它应该像您期望的那样显示在“/swagger”中。

Try to use a relative path to SwaggerEndpoint:尝试使用 SwaggerEndpoint 的相对路径:

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

And then navigate to {App Url}/swagger.然后导航到 {App Url}/swagger。

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

Try to use a relative path to SwaggerEndpoint:尝试使用 SwaggerEndpoint 的相对路径:

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

For experiment try adding the next line in your ~\Properties\launchSettings.json file:对于实验,请尝试在~\Properties\launchSettings.json文件中添加下一行:

"launchUrl": "swagger/index.html"

Finally it will be look like this:最后它看起来像这样:

在此处输入图像描述

In Startup.cs remain app.UseSwaggerUI with the next parameters:Startup.cs中,使用以下参数保留app.UseSwaggerUI

app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("v1/swagger.json", "API V1");
        c.InjectStylesheet("../swagger-ui/custom.css");
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 显示 Swagger.json 但 UI 不显示 - Swagger.json is displayed but the UI not Swagger 不生成 swagger.json - Swagger is not generating swagger.json 使用 GenerateSchema 添加的类出现在 Swagger UI 中,但不在 swagger.json 中 - Classes Added Using GenerateSchema Appear in the Swagger UI But Not in swagger.json 当本地主机提供 swagger.json 时,Autorest 无法针对文件解析 swagger.json - Autorest Failed resolving swagger.json against file when swagger.json is served by localhost Blazor Swagger:无法加载 API 定义。 获取错误:未定义 /swagger/v1/swagger.json - Blazor Swagger: Failed to load API definition. Fetch error: undefined /swagger/v1/swagger.json SWAGGER UI 不显示 - Swagger.json 在 .NET Core 上显示 - SWAGGER UI does not show up - Swagger.json does on .NET Core 未能加载 API 定义,获取错误:未定义 /swagger/v1/swagger.json - Failed to load API definition, Fetch error: undefined /swagger/v1/swagger.json ASP.NET Core Web API - Fetch error undefined /swagger/MyApp API v1/swagger.json - ASP.NET Core Web API - Fetch error undefined /swagger/MyApp API v1/swagger.json 如何按控制器名称对 NSwag swagger.json 中的路径进行排序/排序 - How to order/sort paths in NSwag swagger.json by controller name 我没有在 swagger.json 中得到方法描述 - I'm not getting a method description in swagger.json
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM