可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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">
....
回答1:
It could be that you haven't added a reference to SignalR.AspNet.dll
. If I recall correctly it's responsible for adding the route to /signalr/hubs
.
回答2:
Try call RouteTable.Routes.MapHubs() before RouteConfig.RegisterRoutes(RouteTable.Routes) in Global.asax.cs if you use MVC 4. It works for me.
RouteTable.Routes.MapHubs();
RouteConfig.RegisterRoutes(RouteTable.Routes);
回答3:
From the SignalR 1.0.0 RC2 there is a README in the packages folder that says the Hubs route must be manually established. :) Here is a snippet...
using System;
using System.Web;
using System.Web.Routing;
namespace MyWebApplication
{
public class Global : System.Web.HttpApplication
{
public void Application_Start()
{
// Register the default hubs route: ~/signalr
RouteTable.Routes.MapHubs();
}
}
}
回答4:
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();
}
}
}
回答5:
I've struggled with this problem too and finally got to the point that was causing the problem. First of all, I have to say that with SignalR v2 callingRouteTable.Routes.MapHubs();
inside Globalasax/Application_Start is obsolete and compiler even throws warning. Instead we now add a dedicated StartUp class with the following public method:
public void Configuration(IAppBuilder app)
{
app.MapSignalR();
}
All the configuration goes here.See the documentation for more info.
Now, after wasting many hours googling like a crazy I decided to throw an Exception inside the Configure method of the StartUp class I mentioned earlier. If no exception would be thrown then I'd understand that Owin does not even start. My guess was right. For some reason Owin was not starting or something was suppressing it. In my case it was this evil configuration setting in my web.config file:
<add key="owin:AutomaticAppStartup" value="false" />
I guess the name of the setting is pretty descriptive. Either remove this or change false to true.
回答6:
Have you just installed IIS? In this case you might have to reinstall it:
c:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -i
回答7:
I had this same problem when running my code using the Visual Studio development server and it worked when I changed my project settings to use IIS Local Web Server.
There was a defect raised against this issue which David Fowler commented on. The problem will be fixed in future releases but right now this is the workaround. I cant find the link to the bug at the moment.
回答8:
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.
回答9:
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:
- Microsoft.AspNet.SignalR.SystemWeb
- 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.
回答10:
This is a full answer from SignalR wiki (https://github.com/SignalR/SignalR/wiki/Faq). It worked with me:
First, make sure you have a call to MapHubs() in Global.asax.
Please make sure that the Hub route is registered before any other routes in your application.
RouteTable.Routes.MapHubs();
In ASP.NET MVC 4 you can do the following:
<script type="text/javascript" src="~/signalr/hubs"></script>
If you're writing an MVC 3 application, make sure that you are using Url.Content for your script references:
<script type="text/javascript" src="@Url.Content("~/signalr/hubs")"></script>
If you're writing a regular ASP.NET application use ResolveClientUrl for your script references:
<script type="text/javascript" src='<%= ResolveClientUrl("~/signalr/hubs") %>'></script>
If the above still doesn't work, make sure you have RAMMFAR set in your web.config:
<configuration>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
</modules>
</system.webServer>
</configuration>
回答11:
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
回答12:
I had this 404 errors when i updated to Signalr 2.0 and deployed MVC project to the production server. Publishing project with the "delete all existing files prior to publish" option saved my problems.
hope this helps someone.
回答13:
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>
回答14:
Perhaps worth mentioning: I'm running a Web Forms application that uses it's own manual routing. Since OWIN startup occurs after the routing tables have been set, the route for /signalr/hubs was never hit. I added a rule to ignore routes (IE: let web forms do the routing) on any path that starts with "/signalr". This solved my issue.
回答15:
I solved this issue by switching the application pool .NET Framework Version from v2.0 to v4.0.
回答16:
may be because your hub class name is 'Test' and you are referring to 'test' in client side.
回答17:
you dont need the signalr/hubs file it is just to have easier debugging and straightforward way to call a function. see : See what the generated proxy does for you , that is all. Things will work without it.
回答18:
For me the solution was to reinstall all the packages and restore all the dependecies.
Open nuget powershell and use this command.
Update-Package -Reinstall
回答19:
adding app.MapSignalR();
in Startup
class resolves the issue