[英]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时,此页面的文本框为空:
我希望这可以根据环境动态发生。 例如,如果我访问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.