/signalr/hubs not loading in asp.net mvc4: Throws 404

Pitamber Tiwari picture Pitamber Tiwari · Sep 27, 2012 · Viewed 21.1k times · Source

Here is what I did.

  1. I used nuget to get the SignalR for my MVC4 project.
  2. Created a MyHub class in my controller (SignalRTestController.cs)
  3. In the Index Action, tried to broadcast a message from outside the hub and returned the view.
  4. In the View, referenced all the scripts and /signalr/hubs.

Problem is /signalr/hubs not being found (throws 404).

My project has areas and is structured as shown:

  1. MVCProject
    • Areas
      • SubFolder
        • Controller
          • SignalRTestController.cs
        • Model
        • View
          • Index.cshtml
    • Controller
    • Model
    • View
    • Scripts

All the scripts for signalR are inside the Scripts folder and my SignalRTestController.cs looks like this:

namespace SignalRTest.Controllers
{
public class SignalRTestController : Controller
{
    public ActionResult Index()
    {
        // Do some work here

        // Broadcasting over a Hub from outside of a Hub
        var context = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
        context.Clients.say("Hello SignalR!");

       return View();
    }
}

[HubName("MyHub")]
public class MyHub : Hub
{
    public void Say(string message)
    {
        Clients.sendMessage(message);
    }
}
}

Any my Index.cshtml has reference to all the javascripts and the /signalr/hubs too like below: // Other Javascripts

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

I think the controller is fine, but I'm not getting /signalr/hubs. It is throwing 404 and the message in Chrome Console is like this:

Resource interpreted as Script but transferred with MIME type text/html: "http://www.myproject.com/signalr/hubs". Uncaught SyntaxError: Unexpected token < hubs:2 Uncaught SignalR: Error loading hubs. Ensure your hubs reference is correct, e.g. script src='/signalr/hubs'>.

The reason the script is returned as html is the server is returning 404 not found.

I'm not sure what is wrong. I think this might be a routing issue. I'm not sure, if we need to add any routing information on the project for /signalr/hubs or am I missing something here.

FYI: When I create a new empty MVC project and add signalR and start working on it, it works perfectly fine. No need to add routing.

Also, I use both default routing and attribute routing in some places. But the controller SignalRTestController does not use attribute routing.

Answer

Pete picture Pete · Dec 7, 2012

Do you have a call to RouteTable.Routes.MapHubs() (probably in Global.asax)? If so, try getting rid of that and seeing if it fixes your problem. – Pete Nov 16 at 17:22

I've been playing with it some more. It appears that in the current version (I got the latest source because I needed a signed assembly), you have to call RouteTable.Routes.MapHubs(). But for it to work, it had to be called first (or at least before the RouteConfig.RegisterRoutes() call). If it was called after that, MVC goes hunting for a controller for it and that doesn't work. In the earlier version that I was using that got via nuGet, removing RouteTable.Routes.MapHubs() worked to fix the problem, but it now appears to be required. Hope that's helpful. - Pete Nov 27 at 20:53


Apparently you solved the problem by changing RouteTable.Routes.MapHubs() to: RouteTable.Routes.MapHubs("~/signalr").