Swashbuckle.aspnetcore: Default route issue http://localhost:port/swagger 404 error since VS2017 15.5.1

Created on 14 Dec 2017  路  30Comments  路  Source: domaindrivendev/Swashbuckle.AspNetCore

I recently upgraded Visual Studio 2017 15.5.1.

So far everything was working fine developing .NET Core (2.0) web apis but after the updates the problems started. The problems occur on existing projects as well as newly created ones.

When I now configure the App URL settings in the project properties to start at http://localhost:50000/swagger the browser opens and shows the wrong url, namely http://localhost:50000/swagger/api/values with the correct response of the ValuesController but not the swagger ui. At this point it shouldn't have the extension /api/values since it is not configured and I don't know why or where this is added (it's not in the configuration).

If I now enter (without stopping the app) http://localhost:50000/swagger I start receiving 404 errors an urls, even on http://localhost:50000/api/values.

If I now enter (without stopping the app) http://localhost:50000/swagger/swagger !!! UI shows but with issue about swagger.json of course.

Firing end points at this step seems to work but with strange 500.19 for somes. At this step no breakpoint in the debug are active and app is not stopping anymore on any breakpoints.

Nothing in configuration have changed only upgrade to VS2017 15.5.1.

Any idea to fix ?

Someone else have issue ?

Most helpful comment

For those still experiencing this issue:

  1. Change the application URL back to your default path without '/swagger'.
  2. Add 'swagger' to the 'Launch Browser' property (right click project, properties -> Debug).
  3. Open .vs/config/applicationhost.config and remove all application entries with the '/swagger' path on your site.

Should now work as expected

What was happening:
Swagger was appending the route to the end of your application path, if your application path is set as localhost:53250/swagger for example, then the swagger application will be {appUrl}/swagger which in this case would be localhost:53250/swagger/swagger.
VS have a property 'Launch Browser' which will leave the default application URL and just append the path which you want to browse, which in this case you want as swagger or your API docs.
VS also saves each and every application path you enter, so if you had once entered the path with a /swagger route, it will still be saved in the .config and hence each time you try to go to {app}/swagger it is going to the apps route, not to the route with /swagger appended.

All 30 comments

You could try to use this in Startup class

c# public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseSwaggerUI(options => { options.RoutePrefix = "swagger/ui"; }); }

Thanks for reply but it has no effect.
I tried all what is possible around RoutePrefix and at the end UI shows with cascading 3 /swagger : http://localhost:50000/swagger/swagger/swagger !!!!!
There is an issue swagger / vs2017 15.5.1 or vs2017 15.5.1 / swagger as nothing changed but it stoped working.
Yes terday evening was at 4 http://localhost:50000/swagger/swagger/swagger/swagger when decide delete .vs directory of my solution to back at least at 2 http://localhost:50000/swagger/swagger....

To let you know i found a temporary fix :
1 - keep starting url in project option : http://localhost:50000/swagger.
2 - in startup.cs :
app.UseSwaggerUI(c =>
{
// force to add another /swagger to fix issue
c.SwaggerEndpoint("/swagger/swagger/v1/swagger.json", "My API V1");
});
3 : manually enter URL in chrome when app launched : http://localhost:50000/swagger/swagger.

Seems to work fine at this step.
App stpp on breakpoint as before.
But in my idea there is something to fix.

+1
SwaggerUI suddenly stopped working after upgrading Visual Studio.
http://localhost:50000/swagger/swagger does the job as a temp workaround.

Had same issue, the swagger/swagger fix works, but is not good. Any one know whats going on?

UPDATE
It seems that swagger is somehow adding the routeprefix of swagger to every endpoint, so my "topics" controller actually is at /swagger/topics....

any updates?

Same here... I'be wasted whole day looking for a solution. This is not because VisualStudio but IIS Express.

Solution:

Update vs to 15.5.3 +
Delete .vs folder
Remember about '/' in path

c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");

I had error with swagger/swagger/swagger so I assumed that it is because IIS express configuration file :)

Thank you sebastianpec!

For what it's worth I can reproduce this consistently now by setting the app url to /swagger/ in the project build settings. Swaggerui then moves to /swagger/swagger and api calls to /swagger/. The strange thing is that the problem persists even if I change the app url back to root unless I also delete the .vs folder per sebastianpec.

Thanks Guys for helping me out with this exact scenario. I lost a full day of trying to debug.
I too updated VS and..bam....no more swagger.

Thanks to everyone for the work around. It worked for me.
1) deleted vs folder
2) changed
app.UseSwaggerUI(s =>
{
s.SwaggerEndpoint("/swagger/v1/swagger.json", "My Project API");
});

to

app.UseSwaggerUI(s =>
{
s.SwaggerEndpoint("/swagger/swagger/v1/swagger.json", "My Project API");
});

Albeit not the perfect scenario, but got me back to coding.
Hope this gets fixed in next version.

I was also experiencing this issue. I couldn't get /swagger to work (would always kick to /swagger/swagger, but I was able to get api/docs to work with the following config:

      // Creates Swagger JSON
      app.UseSwagger(c =>
      {
        c.RouteTemplate = "api/docs/{documentName}/swagger.json";
      });

      // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), specifying the Swagger JSON endpoint.
      app.UseSwaggerUI(c =>
      {
        c.SwaggerEndpoint("/api/docs/v1/swagger.json", "AnnexUI API V1");
        c.RoutePrefix = "api/docs";
      });

It may be worth noting that /swagger was working fine for me, even after updating visual studio. It wasn't until I edited my launchsettings.json file for the first time that this problem cropped up. Reverting the changes did not fix the issue.

Fast fix:

  1. Go to project->properties->debug
  2. change App URL from http://localhost:52120/swagger to http://localhost:52120
  3. Run
  4. In browser go to http://localhost:52120/swagger
    this fix wont change swagger urls, it is important if you have oauth authentication.

For those still experiencing this issue:

  1. Change the application URL back to your default path without '/swagger'.
  2. Add 'swagger' to the 'Launch Browser' property (right click project, properties -> Debug).
  3. Open .vs/config/applicationhost.config and remove all application entries with the '/swagger' path on your site.

Should now work as expected

What was happening:
Swagger was appending the route to the end of your application path, if your application path is set as localhost:53250/swagger for example, then the swagger application will be {appUrl}/swagger which in this case would be localhost:53250/swagger/swagger.
VS have a property 'Launch Browser' which will leave the default application URL and just append the path which you want to browse, which in this case you want as swagger or your API docs.
VS also saves each and every application path you enter, so if you had once entered the path with a /swagger route, it will still be saved in the .config and hence each time you try to go to {app}/swagger it is going to the apps route, not to the route with /swagger appended.

Is this an issue with SB, and if so what needs to change because it鈥檚 not clear to me? Or, can we close the issue now?

As per Greg post 馃憤 here is a FUNCTIONAL launchSettings.json

{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:51564/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Ocuco.Web.Application": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "api/values",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:51565/"
}
}
}

backed to normal declaration in startup.cs
..... configureapp
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
});
....

Delete ".vs" directory locally to ensure VS recreate it correctly and it works...

Then yes issue can be close on my side

The issues is with appliacationhost.config after changing project properties > debug > App url. In my case I had to remove the following node from appliacationhost.config:
<application path="/swagger" applicationPool="Clr4IntegratedAppPool"> <virtualDirectory path="/" physicalPath="C:\src\LM\LM.Web" /> </application>

In swagger 2.4.0 this is still a bug.
Repro steps:

  1. Set App URL to http://localhost:xxxxx/Swagger
  2. Run the app
  3. Navigate to http://localhost:xxxxx/Swagger/Swagger, and find Swagger there, unable to load the swagger json file

Recovery:

  1. Remove Swagger from the App URL
  2. Delete the .vs folder, and allow it to be rebuilt
  3. All is right with the world

Same issue as @JamieMeyerImagine
Deleting the .vs folder worked for me too

Swagger 2.4.O
VS Enterprise 15.7.1

you must deploy .Net Core application to IIS.
follow this guide.
https://neelbhatt.com/2018/01/30/deploy-net-core-application-to-iis-step-by-step-guide/
I was successful

Guys this helped me. The issue is that you need to add / to the end of the url.
Accessinglocalhost:port/swagger/ui without slash at the end will result redirecting to swagger/swagger/ui/ and 404 error.

To make things work, add / at the end of swagger url:
localhost:port/swagger/ui/
(Pay attention to last slash: ui/)

image

Example configuration:

                app.UseSwaggerUI(x =>
                {
                    x.RoutePrefix = "swagger/ui";
                });

I just installed Swashbuckle.AspNetCore 3.0.0. It worked for the first time. I was able to access the documentation via http://localhost:58030/swagger Then I removed the ValuesController (default controller in the VS template) and updated the LaunchUrl in launchSettings.json. Since then I have this same issue. No other changes were made and I use Visual Studio 2017.

I have the same problem, unable to use swagger wtih VS2017. Chrome return ERR_CONNECTION_REFUSED
And yet I followed different tutorials and I created 5 test projects. I feel like I have tried everything.

I had the same problem, Delete vs folder and it is working again. I am sure this is the perfect solution.

The issue seems to be still actual. I have just updated VS to 15.9.3 and Swagger stopped working showing 404.

Configuration as follows:

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info { Title = "Travel API", Version = "v1" });
            });
            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "Travel Miser API V1");
            });

            app.UseMvc();

Steps proceeded:

  • .vs removed
  • libraries reinstalled
  • adding RoutePrefix
    but all above unsuccessful.

Does anyone have any idea?

I am having an issue with swashbuckle(4.0.1) on my .net core web api hosted on kubernetes...
after some time some endpoins on swagger UI disappears, even though they still work when called externally.

Any ideas?

I'm having the same problem with version 4.0.1 and VS2019. Was anyone able to fix it or has any workaround?

PS: I was able to call [/swagger/v1/swagger.json] but UI does not work.

For me, I happened after I moved my project to a different directory. Based upon one of the posts above I inspected by applicationhost.config file and under the section one of the references was not updated to the new location. Updated the value and viola swagger was back.

Mine was a simple delete of the .vs folder. Thanks for all the tips on this page.

even if the issue is in a closed state I would add to the previous answers that the main probleme resides in the configuration od IIS Express associated with visual studio 17.x and up,
You have just to create a new virtual directory with a new port number for the webApi Application after you have installed and configured Swagger on it.

In my case, the following code in the Startup.cs file "if (env.IsDevelopment())" results to false because the IWebHostEnvironment was not recognized as 'Development'. In debug mode with a breakpoint set on the if statement, I can easily see the code inside the if statement being skipped. If I manually force it to execute the code inside the if statement, all works fine.

So, I realize that I was missing the environment variable: "environmentVariables": {"ASPNETCORE_ENVIRONMENT": "Development"} As PhilippeCamou mentioned in his previous link .

Once I added the environment variable, as mentioned below, all works fine.

"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}

},

Was this page helpful?
0 / 5 - 0 ratings