Pitamber Tiwari
Pitamber Tiwari

Reputation: 536

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

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.

Upvotes: 15

Views: 21679

Answers (7)

user735232
user735232

Reputation: 181

In my case, there was a key in web.config appSettings that I had to remove:

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

Upvotes: 0

Lzh
Lzh

Reputation: 3635

MapHubs is now obsolete. Use MapSignalR extension method in the Owin startup class, like this:

[assembly: OwinStartupAttribute(typeof(signalr_test.Startup))]
namespace signalr_test
{
    public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            ConfigureAuth(app);
            app.MapSignalR(); //can take path also see overloads...
        }
    }
}

Upvotes: 5

abelabbesnabi
abelabbesnabi

Reputation: 1817

In my case, I had accidentally created two Hub classes with the same name in different locations (Two classes that derive from Hub). I removed one and it all worked.

Upvotes: 0

Pete
Pete

Reputation: 6743

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").

Upvotes: 32

Josh
Josh

Reputation: 776

I was experiencing the same problem. Here is the fix:

Nuget did not add all the necessary references. I reran this command:

Install-Package Microsoft.AspNet.SignalR -pre

and it added a few more references:

  • Microsoft.AspNet.SignalR.Hosting.Aspnet
  • Microsoft.AspNet.SignalR.Hosting.Common

once that was done, it worked like a champ!

Upvotes: 2

ShaneKm
ShaneKm

Reputation: 21368

You have a problem with reference. do the following:

  1. In you AppStart/Bundle.Config.cs add this:

    bundles.Add(new ScriptBundle("~/bundles/signalR").Include(
        "~/Scripts/jquery.signalR-{version}.js"));
    
  2. In layout.cshtml add this:

    @Scripts.Render("~/bundles/signalR")

  3. On you page that you're using signalR make sure you reference signalR hubs like so:

/signalr/hubs

like it's done on this page: http://blog.devscrum.net/2011/12/getting-started-with-signalr-in-asp-net-mvc/ (section where it says: "In index.cshtml add the following code")

Upvotes: 1

elcid
elcid

Reputation: 574

Try changing [HubName("MyHub")] to [HubName("myHub")] and make sure you update wherever you create the proxy in your javascript. It's worked for me in the past.

Upvotes: 0

Related Questions