SignalR"signalr/hubs"给出404错误

Roc*_*ngh 49 c# asp.net signalr

我在我的项目中使用SignalR(https://github.com/SignalR/SignalR).从这里https://github.com/SignalR/SignalR/wiki/QuickStart-Hubs我明白了如何使用Hubs.但"信号器/集线器"脚本给出了404错误.这是在视图源中的URL:http:// localhost:50378/signalsr/hubs给出404错误

这是我的代码:Hub:

public class Test:Hub
{
    public void Start()
    {
        Caller.guid = Guid.NewGuid();
    }

    public void TestMethod()
    {
        Clients.show("test", Caller.guid);
    }
}
Run Code Online (Sandbox Code Playgroud)

ASPX:

<html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Title</title>
        <script src="../Scripts/jquery-1.6.4.min.js" type="text/javascript"></script>
        <script src="../Scripts/jquery.signalR.js" type="text/javascript"></script>
        <script src="<%= ResolveUrl("~/signalr/hubs") %>" type="text/javascript"></script>
        <script type="text/javascript">

            $(document).ready(function () {
                var test = $.connection.test;
                $("#btnTest").click(function () {
                    test.testMethod();
                });
                test.show = function (text, guid) {
                    if (guid != test.guid) //notify all clients except the caller
                        alert(text);
                };
                $.connection.hub.start(function () { test.start(); });
            });

        </script>
    </head>
    <body>
        <form id="HtmlForm" runat="server">
            <div>

            </div>
        </form>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

Web.config文件:

  <system.webServer>
    <validation validateIntegratedModeConfiguration="false"/>
    <modules runAllManagedModulesForAllRequests="true">
....
Run Code Online (Sandbox Code Playgroud)

Ada*_*amy 44

如果你使用MVC 4,请尝试在Global.asax.cs中的RouteConfig.RegisterRoutes(RouteTable.Routes)之前调用RouteTable.Routes.MapHubs().它适用于我.

        RouteTable.Routes.MapHubs();
        RouteConfig.RegisterRoutes(RouteTable.Routes);
Run Code Online (Sandbox Code Playgroud)

  • `RouteTable.Routes.MapHubs();`现在已经过时了.`app.MapSignalR();`应该在owin启动类中的`IAppBuilder`上调用. (4认同)

Mat*_*rla 29

可能是你没有添加引用SignalR.AspNet.dll.如果我没记错的话,它负责添加路由/signalr/hubs.

  • 我想我现在是Microsoft.AspNet.SignalR.SystemWeb.dll (5认同)
  • 此程序集/库是否已重命名为SignalR.Hosting.AspNet.dll?我使用了Nuget包,但没有看到任何名为SignalR.AspNet.dll的源代码 (3认同)

Mic*_*l R 14

SignalR 1.0.0 RC2中,packages文件夹中有一个README,表示必须手动建立Hubs路由.:)这是一个片段......

using System;
using System.Web;
using System.Web.Routing;

namespace MyWebApplication
{
    public class Global : System.Web.HttpApplication
    {
        public void Application_Start()
        {
            // Register the default hubs route: ~/signalr
            RouteTable.Routes.MapHubs();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Cha*_*ari 14

在我的情况下,404的主要原因是集线器没有正确映射.RouteTable.Routes.MapHubs();现在已经过时了.对于映射中心,您可以创建如下的启动类.

[assembly: OwinStartup(typeof(WebApplication1.Startup))]
namespace WebApplication1
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            // Any connection or hub wire up and configuration should go here
            app.MapSignalR();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Mik*_*yev 14

我也一直在努力解决这个问题,并最终达到导致问题的程度.首先,我不得不说,RouteTable.Routes.MapHubs();在Globalasax/Application_Start内调用SignalR v2 已经过时,编译器甚至会发出警告.相反,我们现在使用以下公共方法添加一个专用的StartUp类:

public void Configuration(IAppBuilder app)
{
    app.MapSignalR();
}
Run Code Online (Sandbox Code Playgroud)

所有配置都在这里.有关详细信息,请参阅文档.

现在,浪费了几个小时后,谷歌搜索疯狂,我决定在我之前提到的StartUp类的Configure方法中抛出一个Exception.如果不会抛出任何异常,那么我就会明白Owin甚至没有开始.我的猜测是正确的.出于某种原因,Owin没有开始或有什么东西压制它.在我的情况下,我的web.config文件中的这个邪恶的配置设置:

    <add key="owin:AutomaticAppStartup" value="false" />
Run Code Online (Sandbox Code Playgroud)

我想这个设置的名称非常具有描述性.删除此项或将false更改为true.