swaggerswagger-uiswagger-2.0asp.net-core-webapi.net-core-2.0

.NET Core 2 and SwashBuckle Swagger UI is not Displaying


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":"davis.andre@gmail.com"
      }
   },
   "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.


Solution

  • 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 ;)