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 12:56

I too came across this same issue on an ASP.NET 4.0 Web Forms application. It worked on development servers, but not in production environments. The solution of ensuring all requests/modules run in managed mode wasn't acceptable for us.

The solution we went down (better for ASP.NET Web Forms) is the following. Add the following section to Web.Config:

<modules>
  <remove name="UrlRoutingModule-4.0" />
  <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" />
  <!-- any other modules you want to run in MVC e.g. FormsAuthentication, Roles etc. -->
</modules>
查看更多
来,给爷笑一个
3楼-- · 2019-01-13 12:57

Long story short: in my case I used R# (ReSharper), right clicked my project that uses SignalR and clicked Refactor => Remove Unused References. Watch out: this is a shoot in the foot. :D

It removed IMPORTANT DLL references from SignalR that are used only during runtime, that is, they are not necessary during compile time. Obviously R# only checks for compile time dependencies.

Namely these 2 references were missing:

  1. Microsoft.AspNet.SignalR.SystemWeb
  2. Microsoft.Owin.Host.SystemWeb (without this one a breakpoint in SignalR OWIN Configuration method won't even be hit).

Removed the NuGet packages and then reinstalled them. Fixed the /hubs 404 issue.

查看更多
孤傲高冷的网名
4楼-- · 2019-01-13 12:59

You need to reference the JavaScript file with @Url.Content, assuming ou're using ASP.NET MVC3

Like:

<script src="@Url.Content("~/Scripts/jquery.signalR.min.js")" type="text/javascript"></script>

See the SignalR FAQ on GitHub

Edit: As Trevor De Koekkoek mentioned in their comment, you do not need to add @Url.Content yourself if you're using MVC4 or later. Simply prepending the uri with ~/ suffices. For more information, check out this other SO question.

查看更多
放荡不羁爱自由
5楼-- · 2019-01-13 13:01

My project is ASP.net 4.0 C# Web App, testing environment is Windows Server 2012.

I had the same issue with 1.0.0 RC2, I did what Michael suggests and it works. Thanks.

@MatteKarla: When install SignalR 1.0.0 RC2 by NuGet, following references are added to project:

  • Microsoft.AspNet.SignalR.Core
  • Microsoft.AspNet.SignalR.Owin
  • Microsoft.AspNet.SignalR.SystemWeb
  • Microsoft.Owin.Host.SystemWeb

I have to add Microsoft.CSharp manually or following error will occurred during compile:

  • Predefined type 'Microsoft.CSharp.RuntimeBinder.Binder' is not defined or imported
查看更多
ゆ 、 Hurt°
6楼-- · 2019-01-13 13:02

In my case, the main reason of this 404 is hubs were not properly mapped. RouteTable.Routes.MapHubs(); is now obsolete. For mapping hubs you can create a startup class as below.

[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();
        }
    }
}
查看更多
一夜七次
7楼-- · 2019-01-13 13:02

adding app.MapSignalR(); in Startup class resolves the issue

查看更多
登录 后发表回答