Swagger not loading – Failed to load API definition: Fetch error undefined

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.

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

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

‘Multiple operations with path ‘some_path’ and method ‘GET’ ‘

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

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"
});

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.

NOTE: Navigating to swagger/v1/swagger.json will give you more details, for me it was causing issue due to undecorated action. This information is mentioned in comment by @MarkD

Hope this helps someone in the future.

Leave a Comment