【问题标题】:ASP.NET Core 2.2 Signalr failing to connect on server as opposed to localhostASP.NET Core 2.2 Signalr 无法连接到服务器而不是 localhost
【发布时间】:2019-01-29 17:03:43
【问题描述】:

我的 Jquery 中有 Signalr 的这个实现

"use strict";
var SessionId = document.getElementById("Id").value;
var connection = new signalR.HubConnectionBuilder().withUrl("/chathub?SessionId=" + SessionId)
    .build();

当我在 localhost 上启动应用程序时,我会在“网络”选项卡中看到此 url

https://localhost:44312/chathub/negotiate?SessionId=c71d63a0

它可以正常加载,我的聊天也正常。

但是,当我将我的应用程序部署到 iis 上时,我得到了这个 url

http://st.live-url.com/chathub/negotiate?SessionId=6075a782

我得到一个 404(未找到)错误。

我可以知道为什么它在直播时会有所不同吗?我必须安装 CORS 才能正常工作吗?

【问题讨论】:

  • 它是一个 asp.net core 2.2 项目
  • 如果与 API 无关,则与 CORS 无关!
  • 在本地主机上工作正常吗?
  • 啊,我明白了。我假设是因为我看到一些信号器实现说要安装它。是的,它可以在 localhost 上正常运行
  • I get a 404 (Not Found) error instead.- 没有得到 Hub 网址,不是吗?

标签: jquery asp.net asp.net-core signalr


【解决方案1】:

这可能与在我的 iis 服务器中,url 后面有网站名称有关。

这是有道理的。那么你就不能使用/chathub。您要么必须使用相对路径(来自 JS 代码所在的文件):

"use strict";
var SessionId = document.getElementById("Id").value;
var connection = new signalR.HubConnectionBuilder()
    .withUrl("../chathub?SessionId=" + SessionId)
    .build();

或者注入您的应用程序所在的虚拟目录的名称(尽管您希望这是可配置的,这样您就不必在产品与开发中手动更改它):

"use strict";
var appRoot = "/myapp";
var SessionId = document.getElementById("Id").value;
var connection = new signalR.HubConnectionBuilder()
    .withUrl(appRoot + "/chathub?SessionId=" + SessionId)
    .build();

还要确保在 IIS 中安装 Websockets 协议。就在这周,这让我很生气。如果您需要这样做,应该这样做(在 PowerShell 中):

Enable-WindowsOptionalFeature -Online -FeatureName IIS-WebSockets

【讨论】:

    猜你喜欢
    • 2018-08-24
    • 1970-01-01
    • 2019-05-20
    • 1970-01-01
    • 1970-01-01
    • 2020-10-15
    • 1970-01-01
    • 1970-01-01
    • 2019-10-12
    相关资源
    最近更新 更多