Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
525 views
in Technique[技术] by (71.8m points)

c# - SignalR "signalr/hubs" giving 404 error

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">
....
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...