Swagger is not Working Asp.net Core how to open swagger ui

Viewed 23631

This is my Startup.cs file

This is my ConfigureService method in Startup.cs. I have modified it exactly according to documentation, but it's not working. I have removed the launch Url, so it's just going on the port and I have not set any routing.

  public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
            services.AddControllers();
            services.ConnectionToACQEs(Configuration);
            services.AddAutoMapper(typeof(Startup));
            // Register the Swagger generator, defining 1 or more Swagger documents
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo
                {
                    Version = "v1",
                    Title = "ToDo API",
                    Description = "A simple example ASP.NET Core Web API",
                    TermsOfService = new Uri("https://example.com/terms"),
                    Contact = new OpenApiContact
                    {
                        Name = "Nicky Liu",
                        Email = "nicky@zedotech.com",
                        Url = new Uri("https://www.zedotech.com"),
                    },
                    License = new OpenApiLicense
                    {
                        Name = "Use under LICX",
                        Url = new Uri("https://example.com/license"),
                    }
                });
            });
        }
    

This is my Configure method:

     public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
       {
           //if (env.IsDevelopment())
           //{
           //    app.UseDeveloperExceptionPage();
           //}


           /// 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");
           });
           //app.UseHttpsRedirection();

           app.UseRouting();

           //app.UseAuthorization();

           app.UseEndpoints(endpoints =>
           {
               endpoints.MapControllers();
           });



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

enter image description here

Right click on your project and select Debug on your left panel and on your lauch broswer (absolute or relatve URL) just leave it empty.

Try cleaning your build and building your project.

If it still doesn't work, make sure that your controller has no method without ActionVerbs, ie: HttpGet, HttpPost and so. Swagger requires all controller methods to have ActionVerbs, except those with [ApiExplorerSettings(IgnoreApi = true)] attribute.

None of your methods should define the route along with the ActionVerb: Do [HttpGet,Route("getuser")] instead of [HttpGet("getuser")]

Related