昂首阔步未加载-无法加载API定义:提取错误未定义 [英] Swagger not loading - Failed to load API definition: Fetch error undefined

查看:209
本文介绍了昂首阔步未加载-无法加载API定义:提取错误未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试结合IIS Express上托管的Web应用程序设置设置. API是使用ASP Net Core构建的.我已按照Microsoft相关帮助页面上有关Swashbuckle和ASP.NET Core的指示进行操作.

Trying to setup swagger in conjunction with a web application hosted on IIS express. API is built using ASP Net Core. I have followed the instructions prescribed on the relevant microsoft help page regarding Swashbuckle and ASP.NET Core.

到目前为止,我已经可以加载swagger页面,并且可以看到我定义的SwaggerDoc正在加载,但是没有API.当前出现以下错误:

Thus far I have got the swagger page to load up and can see that the SwaggerDoc that I have defined is loading, however no API's are present. Currently am getting the following error:

未定义提取错误./swagger/v1/swagger.json"

"Fetch error undefined ./swagger/v1/swagger.json"

public class Startup
{

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        // services.AddDbContext<TodoContext>(opt =>
        // opt.UseInMemoryDatabase("TodoList"));
        services.AddMvc()
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

        // Register the Swagger generator, defining 1 or more Swagger documents
        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new Info { Title = "API WSVAP (WebSmartView)", Version = "v1" });
        });
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {

        // Enable middleware to serve generated Swagger as a JSON endpoint.
        app.UseSwagger();

        // 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();
    }
}

推荐答案

因此,在进行了大量的故障排除后,基本上可以将其归为两件事,但我认为总体上这对将来的其他人可能会有帮助,所以我m发布答案.

So after a lot of troubleshooting it came down to basically two things, but I feel that in general this could be helpful to someone else in the future so I'm posting an answer.

首先-如果您遇到上述错误,最好的方法是通过将以下行添加到Configure()方法中来

First- if ever your stuck with the aforementioned error the best way to actually see whats going on is by adding the following line to your Configure() method

app.UseDeveloperExceptionPage();

现在,如果您导航到"swagger/v1/swagger.json"页面,您应该会看到一些更多信息,这些信息将为您指明有用的方向.

Now if you navigate to the 'swagger/v1/swagger.json' page you should see some more information which will point you in useful direction.

第二个-现在对我来说,错误是与

Second- now for me the error was something along the lines of

使用路径'some_path'和方法'GET'的多次操作"

'Multiple operations with path 'some_path' and method 'GET' '

但是这些API位于依赖库中,因此我无法在定义时应用解决方案.作为一种解决方法,我发现将以下行添加到您的ConfigureServices()方法中解决了该问题

However these API were located inside of dependency libraries so I was unable to apply a solution at the point of definition. As a workaround I found that adding the following line to your ConfigureServices() method resolved the issue

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

最后-毕竟,我能够生成JSON文件,但仍然无法拉出UI.为了使此工作正常,我必须在Configure()中更改终点

Finally- After all that I was able to generate a JSON file but still I wasn't able to pull up the UI. In order to get this working I had to alter the end point in Configure()

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

我不确定为什么这样做是必要的,尽管可能值得注意的是,Web应用程序的虚拟目录托管在IIS上,这可能会起作用.

I'm not sure why this was necessary, although it may be worth noting the web application's virtual directory is hosted on IIS which might be having an effect.

希望这对以后的人有帮助.

Hope this helps someone in the future.

这篇关于昂首阔步未加载-无法加载API定义:提取错误未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆