.NET Core 2 and SwashBuckle Swagger UI is not Displaying
Asked Answered
A

6

11

I have followed a few tutorials and have gotten this to work at work but for some reason I am not able to get the UI to display but the Swagger Json is created. Last tutorial I looked at is here.

My setup is like so:

Nuget Package: Swashbuckle.AspNetCore(1.0.0)

ConfigureServices Method:

services.AddSwaggerGen(options =>
            {
                options.SwaggerDoc("v1",
                    new Info
                    {
                        Title = "MediatR Example",
                        Version = "v1",
                        Description = "Trying out the MediatR library to simplify Request and Response logic.",
                        TermsOfService = "WTFPL",
                        Contact = new Contact
                        {
                            Email = "",
                            Name = "",
                            Url = "https://github.com/CubicleJockey/MediatR-Playground"
                        }
                    }
                );

                var xmlDocFile = Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, @"MediatR-Messages.Api.xml");
                options.IncludeXmlComments(xmlDocFile);
                options.DescribeAllEnumsAsStrings();
            });

Configure Method:

 app.UseMvcWithDefaultRoute();

            // 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(config =>
            {
                config.SwaggerEndpoint("/swagger/v1/swagger.json", "V1 Docs");
            });

launchSettings.json:

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

Running and visiting the Swagger JSON url produces the appropriate JSON:

   {
   "swagger":"2.0",
   "info":{
      "version":"v1",
      "title":"MediatR Example",
      "description":"Trying out the MediatR library to simplify Request and Response logic.",
      "termsOfService":"WTFPL",
      "contact":{
         "name":"André Davis",
         "url":"https://github.com/CubicleJockey/MediatR-Playground",
         "email":"[email protected]"
      }
   },
   "basePath":"/",
   "paths":{
      "/api/Addition":{
         "get":{
            "tags":[
               "Addition"
            ],
            "summary":"Get Methods that takes two numbers and gets the sum.",
            "operationId":"ApiAdditionGet",
            "consumes":[

            ],
            "produces":[
               "text/plain",
               "application/json",
               "text/json"
            ],
            "parameters":[
               {
                  "name":"left",
                  "in":"query",
                  "description":"Left hand side of the equation.",
                  "required":false,
                  "type":"integer",
                  "format":"int32"
               },
               {
                  "name":"right",
                  "in":"query",
                  "description":"Right hand side of the equation.",
                  "required":false,
                  "type":"integer",
                  "format":"int32"
               }
            ],
            "responses":{
               "200":{
                  "description":"Success",
                  "schema":{
                     "$ref":"#/definitions/Task[AdditionResponse]"
                  }
               }
            }
         }
      }
   },
   "definitions":{
      "Task[AdditionResponse]":{
         "type":"object",
         "properties":{
            "result":{
               "$ref":"#/definitions/AdditionResponse",
               "readOnly":true
            },
            "id":{
               "format":"int32",
               "type":"integer",
               "readOnly":true
            },
            "exception":{
               "type":"object",
               "readOnly":true
            },
            "status":{
               "enum":[
                  "Created",
                  "WaitingForActivation",
                  "WaitingToRun",
                  "Running",
                  "WaitingForChildrenToComplete",
                  "RanToCompletion",
                  "Canceled",
                  "Faulted"
               ],
               "type":"string",
               "readOnly":true
            },
            "isCanceled":{
               "type":"boolean",
               "readOnly":true
            },
            "isCompleted":{
               "type":"boolean",
               "readOnly":true
            },
            "isCompletedSuccessfully":{
               "type":"boolean",
               "readOnly":true
            },
            "creationOptions":{
               "enum":[
                  "None",
                  "PreferFairness",
                  "LongRunning",
                  "AttachedToParent",
                  "DenyChildAttach",
                  "HideScheduler",
                  "RunContinuationsAsynchronously"
               ],
               "type":"string",
               "readOnly":true
            },
            "asyncState":{
               "type":"object",
               "readOnly":true
            },
            "isFaulted":{
               "type":"boolean",
               "readOnly":true
            }
         }
      },
      "AdditionResponse":{
         "type":"object",
         "properties":{
            "answer":{
               "format":"int32",
               "type":"integer",
               "readOnly":true
            },
            "equation":{
               "type":"string",
               "readOnly":true
            }
         }
      }
   },
   "securityDefinitions":{

   }
}

When visiting the default Swagger UI url I get a 404. Tried a few variations.

  1. localhost:64881/swagger/
  2. localhost:64881/swagger/ui
  3. localhost:64881/swagger/index.html
  4. localhost:64881/swagger/ui/index.html

All of the above return 404. These have worked before depending on the versions. What am I missing.

My full source code can be found on GitHub here. This is a branch for this question so the code matches my ask.

Apologete answered 23/8, 2017 at 23:15 Comment(0)
R
10

What did the trick for me:

  1. Delete all old files from your deployment folder (I tried with IIS, also works for other hosting types)
  2. Replace all Microsoft.AspNetCore.* packages with Microsoft.AspNetCore.All meta package - see this post for the details.
  3. [optional] Just case of the side effects, reinstall Swashbuckle.AspNetCore package (no other Swashbuckle.AspNetCore.* packages needed)
  4. Make sure you have these 2 packages in the project file (it is enough to make it working):

    • PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0"
    • PackageReference Include="Swashbuckle.AspNetCore" Version="1.0.0"
  5. Publish (or deploy by copying the files) to your deployment folder. Now it should work.

NOTE: sometimes it fails (in this case it shows some unclear error in the browser) if you have duplicating model names in your API ;)

Rosin answered 2/9, 2017 at 19:59 Comment(2)
I also needed to delete the .vs solution folderIvar
For ASP.NET Core 2.1 and above metapackage Microsoft.AspNetCore.All has been replace with Microsoft.AspNetCore.AppRosin
M
11

After downloading and testing your code, it appears you must add the following NuGet package to your project:

Microsoft.AspNetCore.StaticFiles

You can do this through the NuGet Manager or by adding the following line to your .csproj <ItemGroup>

<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.0.0" /> 

Source: https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/438

Manicure answered 24/8, 2017 at 0:58 Comment(3)
Adding Microsoft.AspNetCore.StaticFiles package is not needed if you replaced all Microsoft.AspNetCore.* packages with Microsoft.AspNetCore.All meta package.Rosin
Worked great for me :-) TBH I much prefer this approach than installing Microsoft.AspNetCore.All , which can greatly increase the number of dependencies in a projectIngunna
Package Microsoft.AspNetCore.StaticFiles 2.0.0 is not compatible with netcoreapp1.1.Rounding
R
10

What did the trick for me:

  1. Delete all old files from your deployment folder (I tried with IIS, also works for other hosting types)
  2. Replace all Microsoft.AspNetCore.* packages with Microsoft.AspNetCore.All meta package - see this post for the details.
  3. [optional] Just case of the side effects, reinstall Swashbuckle.AspNetCore package (no other Swashbuckle.AspNetCore.* packages needed)
  4. Make sure you have these 2 packages in the project file (it is enough to make it working):

    • PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0"
    • PackageReference Include="Swashbuckle.AspNetCore" Version="1.0.0"
  5. Publish (or deploy by copying the files) to your deployment folder. Now it should work.

NOTE: sometimes it fails (in this case it shows some unclear error in the browser) if you have duplicating model names in your API ;)

Rosin answered 2/9, 2017 at 19:59 Comment(2)
I also needed to delete the .vs solution folderIvar
For ASP.NET Core 2.1 and above metapackage Microsoft.AspNetCore.All has been replace with Microsoft.AspNetCore.AppRosin
S
5

Try deleting .vs folder. Fixed it for me after upgrading to ASP.NET Core 2.1

Stila answered 18/6, 2018 at 19:37 Comment(0)
C
1

I had the same problem for several hours, but was using a port considered unsafe by Chrome (the text stating this was discreet and easy to miss). This may come in handy for somebody in the future.

Chaparro answered 8/5, 2019 at 15:36 Comment(0)
T
1

I had same issue, I was missing HttpMethod binding.

System.NotSupportedException: Actions require an explicit HttpMethod binding for Swagger 2.0

Tertiary answered 13/10, 2019 at 4:6 Comment(0)
F
1

Clean and build your project again.

Also, make sure that all the methods in your controller has ActionVerbs, ie: HttpGet, HttpPost and so, except those with [ApiExplorerSettings(IgnoreApi = true)] attribute.

ActionVerb and the Route must be separate: Do [HttpGet,Route("getuser")] instead of [HttpGet("getuser")]

Fernandes answered 5/1, 2022 at 7:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.