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条回答
可以哭但决不认输i
2楼-- · 2019-01-13 13:03

I solved this issue by switching the application pool .NET Framework Version from v2.0 to v4.0.

查看更多
在下西门庆
3楼-- · 2019-01-13 13:05

This is a full answer from SignalR wiki (https://github.com/SignalR/SignalR/wiki/Faq). It worked with me:

First, make sure you have a call to MapHubs() in Global.asax.

Please make sure that the Hub route is registered before any other routes in your application.

RouteTable.Routes.MapHubs();

In ASP.NET MVC 4 you can do the following:

<script type="text/javascript" src="~/signalr/hubs"></script>

If you're writing an MVC 3 application, make sure that you are using Url.Content for your script references:

<script type="text/javascript" src="@Url.Content("~/signalr/hubs")"></script>

If you're writing a regular ASP.NET application use ResolveClientUrl for your script references:

<script type="text/javascript" src='<%= ResolveClientUrl("~/signalr/hubs") %>'></script>

If the above still doesn't work, make sure you have RAMMFAR set in your web.config:

<configuration>
   <system.webServer>
        <modules runAllManagedModulesForAllRequests="true">
        </modules>
    </system.webServer>
</configuration>
查看更多
【Aperson】
4楼-- · 2019-01-13 13:07

Try call RouteTable.Routes.MapHubs() before RouteConfig.RegisterRoutes(RouteTable.Routes) in Global.asax.cs if you use MVC 4. It works for me.

        RouteTable.Routes.MapHubs();
        RouteConfig.RegisterRoutes(RouteTable.Routes);
查看更多
Summer. ? 凉城
5楼-- · 2019-01-13 13:08

you dont need the signalr/hubs file it is just to have easier debugging and straightforward way to call a function. see : See what the generated proxy does for you , that is all. Things will work without it.

查看更多
劫难
6楼-- · 2019-01-13 13:09

I had this same problem when running my code using the Visual Studio development server and it worked when I changed my project settings to use IIS Local Web Server.

enter image description here

There was a defect raised against this issue which David Fowler commented on. The problem will be fixed in future releases but right now this is the workaround. I cant find the link to the bug at the moment.

查看更多
女痞
7楼-- · 2019-01-13 13:10

From the SignalR 1.0.0 RC2 there is a README in the packages folder that says the Hubs route must be manually established. :) Here is a snippet...

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();
        }
    }
}
查看更多
登录 后发表回答