Asad Iftikhar
Asad Iftikhar

Reputation: 717

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

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 = "[email protected]",
                        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();
           });



       }

Upvotes: 15

Views: 74716

Answers (3)

MatrixRonny
MatrixRonny

Reputation: 781

In my case, @Sydney_dev's answer did not work. It worked after removing RoutePrefix setting, but let me explain a little.

SwaggerUI is accessible at an URL similar to https://localhost:5826/[RoutePrefix]. When accessed, it returns a permanent redirect to the page where you can test the API. This permanent redirect is stored in the browser and has effect even if RoutePrefix is changed. This may end up redirecting and displaying an empty page with HTTP status 404 (Not Found).

In addition, when you start debugging your application the configuration next to the play button is used, for example UnifiedStockExchange is used in the picture bellow:

enter image description here

This configuration is searched in Properties/launchSettings.json. In my case, the following setting was specified:

"launchUrl": "swagger"

This means that when the application starts debugging it opens an URL similar to https://localhost:5826/swagger. When RoutePrefix is set to empty, SwaggerUI is not located there. Either change launchUrl, either remove RoutePrefix config (default is "swagger"), either change URL after launching the debugger.

Upvotes: 6

Ashique Razak
Ashique Razak

Reputation: 685

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")]

Upvotes: 3

Sydney_dev
Sydney_dev

Reputation: 1380

 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.

Upvotes: 34

Related Questions