1

I took long time to try to configure swagger for my .netCore project.

the following steps I have done, but not successful :-(

  1. Nuget Package:

    Swashbuckler.SwaggerGen,
    Swashbuckler.SwaggerUi,
    
  2. in ConfigureServices Method

    instance.AddMvc();
    instance.AddSwaggerGen();
    
  3. in Config Method

     instance.UseMvc();
     instance.UseSwagger();
     instance.UseSwaggerUi();
    
  4. I rebuild my project, and go to link http://localhost:5000/swagger/ui

came error: localhost refused to connect.ERR_CONNECTION_REFUSED

Can anybody help me??

BR,

Leon

7
  • 2
    Is your application even running on this port? when you run with a newly created project it runs on IIS Express, usually with high random seeming number like 45342
    – Tseng
    Commented Aug 10, 2017 at 11:01
  • Hi Tseng, yes my allication is running on 5000 Commented Aug 10, 2017 at 11:05
  • 1
    Then you shouldn't get connection refused messages and get 200 (all okay), 404 (page not found) or 500 (internal server error, i.e. an exception)
    – Tseng
    Commented Aug 10, 2017 at 11:07
  • Hi Tseng, Do you know, how can I set a port for swagger? Commented Aug 10, 2017 at 11:21
  • You don't. It will be running on the same application. Are you absolutely positively sure that you can reach your application at localhost:5000? If you can, then you should'n get a ERR_CONNECTION_REFUSED. In all likeliness you would get a 404 at /swagger/ui. Have you tried removing the /ui part?
    – Marcus W
    Commented Aug 10, 2017 at 11:29

2 Answers 2

2

This is what I have in my aspnet core rest api project.

In ConfigureServices method:

services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1", new Info
    {
        Title = "MyProject - HTTP API",
        Version = "v1",
        Description = "My project HTTP API."
    });
});

In the Configure method:

app.UseSwagger().UseSwaggerUI(c =>
{
    c.SwaggerEndpoint("/swagger/v1/swagger.json", "MyProj HTTP API v1");
});

Then simply navigate to http://localhost:56468/swagger/ where 56468 is the port my application is running on.

2
0

You should include NuGet package Swashbuckle.AspNetCore for swagger configuration. Add below code in Configure section of Startup.cs

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

And add below code in ConfigureServices section of Statup

services.AddSwaggerGen(c => {
       c.SwaggerDoc("v1", new OpenApiInfo() { Title = "My API", Description = "My API V1" });
    });

Not the answer you're looking for? Browse other questions tagged or ask your own question.