Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

/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.

like image 844
Pitamber Tiwari Avatar asked Sep 27 '12 15:09

Pitamber Tiwari


People also ask

Does SignalR need SSL?

If your SignalR application transmits sensitive information between the client and server, use SSL for the transport.

Is SignalR an RPC?

SignalR provides an API for creating server-to-client remote procedure calls (RPC). The RPCs invoke functions on clients from server-side . NET Core code.

Does SignalR use https?

You can run SignalR on Https. SSL is a host concern, not really related to SignalR. Please check below mentioned links for more info.


2 Answers

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

like image 146
Pete Avatar answered Oct 12 '22 10:10

Pete


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...
        }
    }
}
like image 41
Mzn Avatar answered Oct 12 '22 11:10

Mzn