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

114,201

Solution 1

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.

Solution 2

I've been working with .Net Core 3.1 and I spent some time to find out and understanding what was going on.

The issue can arise from many different reasons:

enter image description here

  1. Swagger configuration errors

  2. Classes with the same name but in different namespaces

  3. Public methods without the rest attribute (Get, Post, etc.)

First, take a look the link below just to check if your setup is ok:

Add Swagger(OpenAPI) API Documentation in ASP.NET Core 3.1

Then,

A good tip to find out the problem is to run the application without to use IISExpress and check the console log. Any error found to generate the documentation will be displayed there.

In my case, the problems was that I had a public method (that should be private) without any rest attribute:

enter image description here

After change the method from public to private I solve the issue.

Solution 3

I was able to find the error by opening the network tab and looking at the response for swagger.json

enter image description here

Solution 4

Simply navigate to https://localhost:{PortNo}/swagger/v1/swagger.json and get much more details about the error message.

Solution 5

I've come across the same error before, after struggling to find the reason, I discovered that one of my API in one of my controllers have no HTTP verb as an attribute, So I fixed it by putting [HttpGet] on my API. So here is my advice, check your API controllers, maybe you forget the same thing as me!

Take a look at my code, I realized that I should change this :

   public async Task<Product> ProductDetail(int id)
    {
        return await _productQueries.GetProductDetail(id);
    } 

to this:

        [Route("ProductDetail")]
        [HttpPost]
        public async Task<Product> ProductDetail(int id)
        {
            return await _productQueries.GetProductDetail(id);
        } 
Share:
114,201
Jeremy S.
Author by

Jeremy S.

Full stack developer dabbling in everything from PostgreSQL to C# and react.

Updated on February 13, 2022

Comments

  • Jeremy S.
    Jeremy S. about 2 years

    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.

    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:

    "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();
        }
    }
    
  • MarkD
    MarkD over 3 years
    navigating directly to the swagger/v1/swagger.json solved this. An undecorated action.
  • levi
    levi over 3 years
    For me it was number three. "Public methods without the rest attribute" You help me very muth. Thanks!
  • Marc Roussel
    Marc Roussel over 3 years
    Another reason, which I just encountered, is when the name of the attribute [HttpPost("NAME_HERE") isn't the same as the action name.
  • Mog0
    Mog0 over 3 years
    The navigation to swagger/v1/swagger.json also gave me the solution to my problem - action needed an explicit [HttpGet] attribute
  • Hopeless
    Hopeless about 3 years
    You are a genius. Just wasted an afternoon not able to find the error thanks.
  • Naod Agere
    Naod Agere about 3 years
    Still a better solution is to decorate the class/controller that has released the error with ApiExplorerSettings (IgnoreApi = true)]
  • Moondog 2112
    Moondog 2112 about 3 years
    For me, it was 'Classes with the same name but in different namespaces'. I have a .Net Core Api and had a 'User' class in two different namespaces. Renaming the second corrected the issue
  • M Fuat
    M Fuat almost 3 years
    Mention the source at least benday.com/2020/12/16/…
  • Júlio Almeida
    Júlio Almeida almost 3 years
    Thanks!!! my problem was a ODataController with the next error: No media types found in 'Microsoft.AspNet.OData.Formatter.ODataInputFormatter.Suppor‌​tedMediaTypes
  • Droobie
    Droobie almost 3 years
    I had a conflicting path problem. This showed it very clearly. Thanks!
  • japes Sophey
    japes Sophey almost 3 years
    Thank you for this, great help in assisting my debugging.
  • Syed Mohamed
    Syed Mohamed almost 3 years
    Navigate to your application's swagger.json in your browser has shown the error clearly. localhost:808/swagger/v1/swagger.json
  • Syed Mohamed
    Syed Mohamed almost 3 years
    this worked, and helped me to found the exception
  • Pranesh Janarthanan
    Pranesh Janarthanan almost 3 years
    adding this line ResolveConflictingActions resolved my issue.
  • Matthew MacFarland
    Matthew MacFarland over 2 years
    I also had change that same endpoint path to "./v1/swagger..." to get it working on the web server. Very strange requirement. The new path works fine in local debug and on the server. Seems like it would be the default from the project template.
  • Mahdi Abyaznezhad
    Mahdi Abyaznezhad over 2 years
    For me, it is number 3. Thanks.
  • JonK
    JonK about 2 years
    for me it was number 2. I ended up having to smurf name things a little which was a little sad but understandable.
  • chiwal
    chiwal about 2 years
    Using dev tools/developer tools is brilliant. Thanks