12

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



       }
fat
  • 6,435
  • 5
  • 44
  • 70
Asad
  • 617
  • 2
  • 8
  • 23
  • 1
    "its not working"... what indicates that this is the case? – spender Jul 29 '20 at 18:11
  • spender The application is working fine one localhost/Acquirer/Dashboard i just want to go to swagger ui – Asad Jul 29 '20 at 18:17
  • 2
    Can you try with adding this 'c.RoutePrefix = string.Empty;' in configure method to app.UseSwaggerUI. By this if you just load your domain (https://example.com) it will redirect to swagger ui – Sowmyadhar Gourishetty Jul 29 '20 at 18:24
  • do you go to `https://localhost:yourport/swagger` or `http://localhost:yourport/swagger`? What do you see on that page? – Julian Jul 29 '20 at 19:35
  • 1
    https://localhost:44309/index.html This was my url @Julian – Asad Jul 30 '20 at 05:00

3 Answers3

28
 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.

Dijkgraaf
  • 11,049
  • 17
  • 42
  • 54
Sydney_dev
  • 1,448
  • 2
  • 17
  • 24
1

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

Ashique Razak
  • 487
  • 3
  • 8
1

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, either change URL after launching the debugger.

MatrixRonny
  • 437
  • 3
  • 11