【问题标题】:Dynamic server baseUrl with .NET MVC and Angular带有 .NET MVC 和 Angular 的动态服务器 baseUrl
【发布时间】:2019-09-26 22:25:23
【问题描述】:

我正在使用应用程序测试一些 SignalR,一切正常。现在要在服务器(azurewebsites)中对其进行测试,我需要将 baseUrl 设为动态并获取当前 url,以便可以建立 SignalR 集线器连接。

现在我有以下连接代码:

//server configuration
export let CONFIGURATION = {
    baseUrls: {
        server: 'http://localhost:51913'
    },
}

SomeMethod()
{
this.connection = $.hubConnection(CONFIGURATION.baseUrls.server);

//... the rest of the process to connect to the hub.
}

显然这不适用于 localhost 作为 URL。如何获取主机 url 以开始我的连接?有什么方法可以获取它或从例如 WebConfig 中读取它?我只是不知道它是否有效。

PS:也许有可能在没有特定网址的情况下连接到我的集线器?

【问题讨论】:

  • 你不知道你上传应用的地址吗?
  • @GaryB 有多个地址,比如build、QA等...
  • 我会把它们放到环境中,它会相应地改变
  • 是的,这是一个很好的方法,我将使用 this.document.location.origin;不会工作。

标签: angular asp.net-mvc signalr


【解决方案1】:

您可以通过以下简单的代码行获取基本网址:

let baseUrl = this.document.location.origin;  

【讨论】:

  • 但是是否适合使用 hubConnection 呢? this.connection = $.hubConnection(this.document.location.origin);
  • 是的,试试看。
  • 出现错误后不起作用:> TypeError: Cannot read property 'document' of undefined
  • 如果您将其放入方法中,则此方法有效。谢谢你的帮助!
  • @Praetorian1995 很高兴它成功了。您可以将其标记为答案:)
【解决方案2】:

如果以后有人有同样的问题:

我会将静态地址放入环境中,它会相应地改变

【讨论】:

  • 是的,这是一种很好的方法,也是一种更优雅的方法。感谢您的回答!
猜你喜欢
  • 2017-11-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-21
  • 1970-01-01
  • 2017-03-08
  • 2021-07-16
  • 1970-01-01
  • 2013-11-23
相关资源
最近更新 更多