SignalR “signalr/hubs” giving 404 error

2019-01-13 12:36发布

I am using SignalR(https://github.com/SignalR/SignalR) in my project. From here https://github.com/SignalR/SignalR/wiki/QuickStart-Hubs I got the idea how to use Hubs. But the "signalr/hubs" script is giving 404 error. Here is the url which becomes in View Source: http://localhost:50378/signalr/hubs giving 404 error

Here is my code: Hub:

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

    public void TestMethod()
    {
        Clients.show("test", Caller.guid);
    }
}

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>

Web.config:

  <system.webServer>
    <validation validateIntegratedModeConfiguration="false"/>
    <modules runAllManagedModulesForAllRequests="true">
....

19条回答
爷的心禁止访问
2楼-- · 2019-01-13 13:11

I've struggled with this problem too and finally got to the point that was causing the problem. First of all, I have to say that with SignalR v2 callingRouteTable.Routes.MapHubs(); inside Globalasax/Application_Start is obsolete and compiler even throws warning. Instead we now add a dedicated StartUp class with the following public method:

public void Configuration(IAppBuilder app)
{
    app.MapSignalR();
}

All the configuration goes here.See the documentation for more info.

Now, after wasting many hours googling like a crazy I decided to throw an Exception inside the Configure method of the StartUp class I mentioned earlier. If no exception would be thrown then I'd understand that Owin does not even start. My guess was right. For some reason Owin was not starting or something was suppressing it. In my case it was this evil configuration setting in my web.config file:

    <add key="owin:AutomaticAppStartup" value="false" />

I guess the name of the setting is pretty descriptive. Either remove this or change false to true.

查看更多
倾城 Initia
3楼-- · 2019-01-13 13:11

For me the solution was to reinstall all the packages and restore all the dependecies.

Open nuget powershell and use this command.

Update-Package -Reinstall
查看更多
仙女界的扛把子
4楼-- · 2019-01-13 13:13

Have you just installed IIS? In this case you might have to reinstall it:

c:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -i
查看更多
The star\"
5楼-- · 2019-01-13 13:16

may be because your hub class name is 'Test' and you are referring to 'test' in client side.

查看更多
干净又极端
6楼-- · 2019-01-13 13:17

I had this 404 errors when i updated to Signalr 2.0 and deployed MVC project to the production server. Publishing project with the "delete all existing files prior to publish" option saved my problems.

hope this helps someone.

查看更多
霸刀☆藐视天下
7楼-- · 2019-01-13 13:19

Perhaps worth mentioning: I'm running a Web Forms application that uses it's own manual routing. Since OWIN startup occurs after the routing tables have been set, the route for /signalr/hubs was never hit. I added a rule to ignore routes (IE: let web forms do the routing) on any path that starts with "/signalr". This solved my issue.

查看更多
登录 后发表回答