首页 文章

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

提问于
浏览
0

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

但是当我去这个位置http://localhost:1234/swagger时,它并没有自动指向http://localhost:1234/swagger/index.html .

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

enter image description here

1 回答

  • 1

    回答你的第一个问题:

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

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

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

    <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>
    

相关问题