Swagger UI - 如何传递基于环境的 URI?

Swagger UI - How to pass environment based URIs?

我在我的 Web API 项目中包含了 Swagger UI 的 [dist] 文件夹。

但是当我去这个地方的时候 http://localhost:1234/swagger 它不会自动指向 http://localhost:1234/swagger/index.html

因此,当我直接访问 http://localhost:1234/swagger/index.html 时,我得到的页面是一个空文本框:----

  1. 我想用 http://localhost:1234/swagger/docs/v1

  2. 填充文本框
  3. 我希望它根据环境动态发生。例如,如果我自动访问 http://test.abc.com/swagger the text should be populated as http://test.abc.com/swagger/docs/v1

回答你的第一个问题:

在您的 index.html 页面中,您可以拥有以下内容:

<script type="text/javascript">

  $(function() {
    window.swaggerUi = new SwaggerUi({
      url: "http://localhost:1234/swagger/docs/v1",
      ...
  });

</script>

至于根据您访问的网站动态生成 url,您可以执行以下操作:

<script type="text/javascript">

  // Initialize the url variable
  if (!window.location.origin) {
    window.location.origin = window.location.protocol + "//" +
      window.location.hostname + (window.location.port ? ':' +
        window.location.port : '');
  }

  $(function() {
    // Create the generated url
    var myUrl = window.location.origin + '/docs/v1';

    // Initialize the Swagger object
    window.swaggerUi = new SwaggerUi({
      url: myUrl,
      ...
    });
  });

</script>