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">
....
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:
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:
SignalR
OWIN Configuration method won't even be hit).Removed the NuGet packages and then reinstalled them. Fixed the
/hubs
404 issue.You need to reference the JavaScript file with
@Url.Content
, assuming ou're using ASP.NET MVC3Like:
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.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:
I have to add Microsoft.CSharp manually or following error will occurred during compile:
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.adding
app.MapSignalR();
inStartup
class resolves the issue