繁体   English   中英

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. 我希望这可以根据环境动态发生。 例如,如果我访问http://test.abc.com/swagger,则该文本应自动填充为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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM