Error when implementing Swagger

Asked

Viewed 75 times

0

I am trying to implement Swagger in my application but I got the error by uploading the application

Erro Swagger

I did it by following the microsoft documentation, but, I did not succeed. It seems a very generic error, so I ask for help. Here’s my Swagger configuration class:

public static class SwaggerConfig
    {
        public static IServiceCollection AddSwaggerConfig(this IServiceCollection services)
        {
            services.AddSwaggerGen(c => 
            {
                c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo
                {
                    Version = "v1",
                    Title = "TestApi",
                    Description = "Primeira versão da API"
                });
            });
            return services;
        }

        public static IApplicationBuilder UseSwaggerConfig(this IApplicationBuilder app)
        {
            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("swagger.json", "v1");
            });
            return app;
        }
    }

My Startup:

        public void ConfigureServices(IServiceCollection services)
        {
...
            services.AddControllers();
            
            services.AddSwaggerConfig();
..
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
{
  

    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        
    }

    app.UseRouting();

    app.UseCors("MyPolicy");
   
    app.UseSwaggerConfig();
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
    });


}

}

  • What happens when you try to access https:/localhost:5001/Swagger/Swagger.json from the browser?

  • HTTP ERROR 404

  • what it looks like is that he is looking for Swagger.json on the /Swagger/Swagger.json path, but in c.swaggerEndpoint you just marked "Swagger.json"

  • If I raise one Swagger at endpoint, it increases a Swagger in the browser. That is, swagger/swagger.jon in endpoint, in the browser I have swagger/swagger/swagger.json

  • take a look at the visual studio in the output window which Exception it is launching, it may be something related in the controllers and not in the configuration

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.