You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SignalR Hub URL在生产服务器返回Fallback HTML而非JSON问题排查

ASP.NET Core SignalR部署后Hub返回Fallback HTML而非JSON响应

本地环境下ASP.NET Core + SignalR项目的Hub可正常访问运行,但部署到生产服务器后,SignalR Hub的URL意外返回Fallback HTML页面,而非预期的JSON响应,导致连接协商失败。

Hub代码

public class NotificationHub: Hub
{
    private readonly ILogger<NotificationHub> _logger;

    public NotificationHub(ILogger<NotificationHub> logger)
    {
        _logger = logger;
    }

    public override async Task OnConnectedAsync()
    {
        var httpContext = Context.GetHttpContext();
        var userId = httpContext.Session.GetString("UserId");
        if (!string.IsNullOrEmpty(userId))
        {
            await Groups.AddToGroupAsync(Context.ConnectionId, userId);
        }
        _logger.LogInformation("Connection established. ConnectionId: {ConnectionId}", Context.ConnectionId);
        await base.OnConnectedAsync();
    }

    public override async Task OnDisconnectedAsync(Exception exception)
    {
        var httpContext = Context.GetHttpContext();
        var userId = httpContext.Session.GetString("UserId");
        if (!string.IsNullOrEmpty(userId))
        {
            await Groups.RemoveFromGroupAsync(Context.ConnectionId, userId);
        }
        _logger.LogInformation("Connection disconnected. ConnectionId: {ConnectionId}, Exception: {Exception}", Context.ConnectionId, exception?.Message);
        await base.OnDisconnectedAsync(exception);
    }
}

报错信息

"Failed to complete negotiation with the server: SyntaxError: Unexpected token '<', "<--  --> "... is not valid JSON"

日志情况

  • 请求返回状态码200,未触发错误日志
  • 本地可正常输出SignalR相关日志,但生产服务器无任何连接及SignalR日志

IIS日志

2024-06-05 09:01:22 10.10.7.51 POST /Webex handler=Connect 80 - 10.55.30.80 Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/125.0.0.0+Safari/537.36+Edg/125.0.0.0 http://myaddress/Webex 200 0 0 593
2024-06-05 09:01:22 10.10.7.51 POST /notificationHub/negotiate negotiateVersion=1 80 - 10.55.30.80 Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/125.0.0.0+Safari/537.36+Edg/125.0.0.0 http://myaddress/Webex?handler=Connect 200 0 0 14

已排查项

  • 服务器不支持WebSocket,但确认这不是返回Fallback页面的原因
  • 已排查URL重写规则,确保应用路由配置与本地一致

寻求相关排查思路。

内容的提问来源于stack exchange,提问作者Ming222

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 23:33:13