无法从Swagger UI中的文件问题中读取

Abi*_*i P 65 swagger-ui swashbuckle

我在我的应用程序中加入了swagger UI.

当我尝试看到swagger UI时,我很好地获得了API的文档但是在一段时间后它在按钮上显示了一些错误图标.

错误消息如下所示:

[{"level":"error","message":"无法从文件中读取 http:// MYIP/swagger/docs/v1 "}]

我不确定是什么导致它.如果我刷新它工作并在几秒后显示错误.

Jon*_*n R 93

我猜" http:// MYIP/swagger/docs/v1 "无法公开访问.

默认情况下,swagger ui使用在线验证器:online.swagger.io.如果它无法访问您的swagger网址,那么您将看到该错误消息.

可能的解决方案:

  1. 禁用验证:

    config.EnableSwagger().EnableSwaggerUi(c => c.DisableValidator());

  2. 使您的网站可公开访问

  3. 在本地托管验证器:

您可以从以下网址获取验证器:https://github.com/swagger-api/validator-badge#running-locally

您还需要告诉swaggerui验证器的位置

config.EnableSwagger().EnableSwaggerUi(c => c.SetValidatorUrl(<validator_url>));

  • 您能否告诉我们哪个文件可以配置验证器被禁用? (3认同)
  • 这取决于您使用的是哪个框架.例如,在Startup.cs中使用Web Api 2:`public void Configuration(IAppBuilder app){... HttpConfiguration config = new HttpConfiguration(); SwaggerConfig.Register(配置); 然后可以在SwaggerConfig.cs中配置验证器 (3认同)

Jus*_*tte 18

为了补充已接受的答案......我只是在SwaggerConfig.cs中取消注释了一行.我只想通过禁用验证器来摆脱主要招摇页面上的红色错误.

// By default, swagger-ui will validate specs against swagger.io's online validator and display the result
// in a badge at the bottom of the page. Use these options to set a different validator URL or to disable the
// feature entirely.
//c.SetValidatorUrl("http://localhost/validator");
c.DisableValidator();
Run Code Online (Sandbox Code Playgroud)


Atu*_*hav 7

如果您使用的是来自swagger-uigithub repo的文件,则可以index.html通过将其设置validatorUrl为来禁用文件中的模式验证null

window.onload = function() {

  // Build a system
  const ui = SwaggerUIBundle({
    url: "/docs/open_api.json",
    dom_id: '#swagger-ui',

    validatorUrl : null,   # <----- Add this line

    deepLinking: true,
    presets: [
      SwaggerUIBundle.presets.apis,
      SwaggerUIStandalonePreset
    ],
    plugins: [
      SwaggerUIBundle.plugins.DownloadUrl
    ],
    layout: "StandaloneLayout"
  })
Run Code Online (Sandbox Code Playgroud)