Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.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":"[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.

like image 228
Cubicle.Jockey Avatar asked Aug 23 '17 23:08

Cubicle.Jockey


People also ask

How do I connect to Swagger UI?

Go to http://localhost:8000/ in your address bar. This address lets you view the local web server. By default, web servers default to the index. html file in the directory, so it will show the Swagger UI file automatically.


1 Answers

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

like image 153
Kyle Polansky Avatar answered Oct 28 '22 00:10

Kyle Polansky