.NET Core 2和SwashBuckle Swagger UI未显示

时间:2017-08-23 23:15:41

标签: swagger swagger-ui swagger-2.0 asp.net-core-webapi .net-core-2.0

我已经按照一些教程进行了工作,但由于某种原因,我无法显示UI,但是创建了Swagger Json。我看过的最后一个教程是here

我的设置是这样的:

Nuget Package: Swashbuckle.AspNetCore(1.0.0)

ConfigureServices方法:

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方法:

 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"
      }
    },

运行和访问Swagger JSON url会生成相应的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":{

   }
}

访问默认的Swagger UI网址时,我得到了404.尝试了一些变体。

  1. 本地主机:64881 /招摇/
  2. 本地主机:64881 /招摇/ UI
  3. 本地主机:64881 /招摇/ index.html中
  4. 本地主机:64881 /招摇/ UI / index.html中
  5. 以上所有都返回404.这些都取决于版本。我错过了什么

    我的完整源代码可以在GitHub上找到here。这是这个问题的一个分支,所以代码符合我的要求。

5 个答案:

答案 0 :(得分:10)

下载并测试您的代码后,您似乎必须将以下NuGet包添加到您的项目中:

Microsoft.AspNetCore.StaticFiles

您可以通过NuGet管理器或将以下行添加到.csproj <ItemGroup>

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

来源:https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/438

答案 1 :(得分:8)

我的诀窍是什么:

  1. 删除部署文件夹中的所有旧文件(我尝试使用IIS,也适用于其他托管类型)
  2. 将所有Microsoft.AspNetCore.*个软件包替换为Microsoft.AspNetCore.All元软件包 - 有关详细信息,请参阅this post
  3. [可选] 如果出现副作用,请重新安装Swashbuckle.AspNetCore包(不需要其他Swashbuckle.AspNetCore.*个包裹)
  4. 确保项目文件中包含这2个包(这足以使其正常工作):

    • PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0"
    • PackageReference Include="Swashbuckle.AspNetCore" Version="1.0.0"
  5. 发布(或通过复制文件进行部署)到部署文件夹。 现在它应该工作。

  6. 注意:如果您的API中有重复的模型名称,有时会失败(在这种情况下,它会在浏览器中显示一些不明确的错误);)

答案 2 :(得分:3)

尝试删除.vs文件夹。在升级到ASP.NET Core 2.1之后为我修复了它

答案 3 :(得分:0)

我在几个小时内遇到了同样的问题,但是使用的端口被Chrome浏览器认为是不安全的(文字说明这是谨慎的,很容易遗漏)。 将来对某些人来说这可能会派上用场。

答案 4 :(得分:0)

我有同样的问题,我缺少HttpMethod绑定。

System.NotSupportedException:动作需要Swagger 2.0的显式HttpMethod绑定