简体   繁体   English

SignalR“signalr/hubs”给出 404 错误

[英]SignalR “signalr/hubs” giving 404 error

I am using SignalR(https://github.com/SignalR/SignalR) in my project.我在我的项目中使用 SignalR(https://github.com/SignalR/SignalR)。 From here https://github.com/SignalR/SignalR/wiki/QuickStart-Hubs I got the idea how to use Hubs.从这里https://github.com/SignalR/SignalR/wiki/QuickStart-Hubs我得到了如何使用集线器的想法。 But the "signalr/hubs" script is giving 404 error.但是“signalr/hubs”脚本给出了 404 错误。 Here is the url which becomes in View Source: http://localhost:50378/signalr/hubs giving 404 error这是在查看源中变为的 url: http://localhost:50378/signalr/hubs给出 404 错误

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

Try call RouteTable.Routes.MapHubs() before RouteConfig.RegisterRoutes(RouteTable.Routes) in Global.asax.cs if you use MVC 4. It works for me.如果您使用 MVC 4,请尝试在 Global.asax.cs 中的 RouteConfig.RegisterRoutes(RouteTable.Routes) 之前调用 RouteTable.Routes.MapHubs()。它对我有用。

        RouteTable.Routes.MapHubs();
        RouteConfig.RegisterRoutes(RouteTable.Routes);

It could be that you haven't added a reference to SignalR.AspNet.dll .可能是您没有添加对SignalR.AspNet.dll的引用。 If I recall correctly it's responsible for adding the route to /signalr/hubs .如果我/signalr/hubs话,它负责将路由添加到/signalr/hubs

From the SignalR 1.0.0 RC2 there is a README in the packages folder that says the Hubs route must be manually established.SignalR 1.0.0 RC2 开始,packages 文件夹中有一个自述文件,说明必须手动建立集线器路由。 :) 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();
        }
    }
}

In my case, the main reason of this 404 is hubs were not properly mapped.就我而言,这个 404 的主要原因是集线器没有正确映射。 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();
        }
    }
}

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 calling RouteTable.Routes.MapHubs();首先,我不得不说,使用 SignalR v2 调用RouteTable.Routes.MapHubs(); inside Globalasax/Application_Start is obsolete and compiler even throws warning. Globalasax/Application_Start 内部已过时,编译器甚至会发出警告。 Instead we now add a dedicated StartUp class with the following public method:相反,我们现在使用以下公共方法添加一个专用的 StartUp 类:

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.现在,在像疯了一样的谷歌搜索上浪费了很多时间之后,我决定在我之前提到的 StartUp 类的 Configure 方法中抛出一个异常。 If no exception would be thrown then I'd understand that Owin does not even start.如果不会抛出任何异常,那么我会理解 Owin 甚至没有开始。 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:就我而言,这是我的 web.config 文件中的这个邪恶的配置设置:

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

I guess the name of the setting is pretty descriptive.我想设置的名称非常具有描述性。 Either remove this or change false to true.删除它或将 false 更改为 true。

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.我在使用 Visual Studio 开发服务器运行我的代码时遇到了同样的问题,当我将项目设置更改为使用 IIS 本地 Web 服务器时,它起作用了。

在此处输入图片说明

There was a defect raised against this issue which David Fowler commented on. David Fowler 评论了针对此问题提出的缺陷。 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.我目前找不到该错误的链接。

Have you just installed IIS?你刚刚安装了 IIS 吗? In this case you might have to reinstall it:在这种情况下,您可能需要重新安装它:

c:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -i

Long story short: in my case I used R# (ReSharper) , right clicked my project that uses SignalR and clicked Refactor => Remove Unused References.长话短说:就我而言,我使用了R# (ReSharper) ,右键单击我使用 SignalR 的项目,然后单击 Refactor => Remove Unused References。 Watch out: this is a shoot in the foot .当心:这是脚中射击 :D :D

It removed IMPORTANT DLL references from SignalR that are used only during runtime, that is, they are not necessary during compile time.它从SignalR中删除了仅在运行时使用的重要 DLL 引用,也就是说,它们在编译时不是必需的。 Obviously R# only checks for compile time dependencies.显然 R# 只检查编译时的依赖关系。

Namely these 2 references were missing:即缺少这 2 个参考文献:

  1. Microsoft.AspNet.SignalR.SystemWeb Microsoft.AspNet.SignalR.SystemWeb
  2. Microsoft.Owin.Host.SystemWeb (without this one a breakpoint in SignalR OWIN Configuration method won't even be hit). Microsoft.Owin.Host.SystemWeb(没有这个, SignalR OWIN 配置方法中的断点甚至不会被命中)。

Removed the NuGet packages and then reinstalled them.删除 NuGet 包,然后重新安装它们。 Fixed the /hubs 404 issue.修复了/hubs 404 问题。

This is a full answer from SignalR wiki ( https://github.com/SignalR/SignalR/wiki/Faq ).这是 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.首先,确保您在 Global.asax 中有对 MapHubs() 的调用。

Please make sure that the Hub route is registered before any other routes in your application.请确保在您的应用程序中的任何其他路由之前注册 Hub 路由。

RouteTable.Routes.MapHubs();

In ASP.NET MVC 4 you can do the following:在 ASP.NET MVC 4 中,您可以执行以下操作:

<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:如果您正在编写 MVC 3 应用程序,请确保您使用 Url.Content 作为您的脚本引用:

<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:如果您正在编写常规 ASP.NET 应用程序,请使用 ResolveClientUrl 作为您的脚本引用:

<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:如果以上仍然不起作用,请确保您在 web.config 中设置了 RAMMFAR:

<configuration>
   <system.webServer>
        <modules runAllManagedModulesForAllRequests="true">
        </modules>
    </system.webServer>
</configuration>

You need to reference the JavaScript file with @Url.Content , assuming ou're using ASP.NET MVC3您需要使用@Url.Content引用 JavaScript 文件,假设您使用的是 ASP.NET MVC3

Like:像:

<script src="@Url.Content("~/Scripts/jquery.signalR.min.js")" type="text/javascript"></script>

See the SignalR FAQ on GitHub请参阅GitHub 上SignalR 常见问题解答

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.编辑:正如 Trevor De Koekkoek 在他们的评论中提到的,如果您使用的是 MVC4 或更高版本,则不需要自己添加@Url.Content Simply prepending the uri with ~/ suffices.简单地在 uri 前面加上~/就足够了。 For more information, check out this other SO question .有关更多信息,请查看其他 SO 问题

My project is ASP.net 4.0 C# Web App, testing environment is Windows Server 2012.我的项目是ASP.net 4.0 C# Web App,测试环境是Windows Server 2012。

I had the same issue with 1.0.0 RC2, I did what Michael suggests and it works.我在 1.0.0 RC2 上遇到了同样的问题,我按照 Michael 的建议做了,而且效果很好。 Thanks.谢谢。

@MatteKarla: When install SignalR 1.0.0 RC2 by NuGet, following references are added to project: @MatteKarla:在通过 NuGet 安装 SignalR 1.0.0 RC2 时,将以下引用添加到项目中:

  • Microsoft.AspNet.SignalR.Core Microsoft.AspNet.SignalR.Core
  • Microsoft.AspNet.SignalR.Owin Microsoft.AspNet.SignalR.Owin
  • Microsoft.AspNet.SignalR.SystemWeb Microsoft.AspNet.SignalR.SystemWeb
  • Microsoft.Owin.Host.SystemWeb Microsoft.Owin.Host.SystemWeb

I have to add Microsoft.CSharp manually or following error will occurred during compile:我必须手动添加 Microsoft.CSharp 否则在编译过程中会出现以下错误:

  • Predefined type 'Microsoft.CSharp.RuntimeBinder.Binder' is not defined or imported未定义或导入预定义类型“Microsoft.CSharp.RuntimeBinder.Binder”

I had this 404 errors when i updated to Signalr 2.0 and deployed MVC project to the production server.当我更新到 Signalr 2.0 并将 MVC 项目部署到生产服务器时,我遇到了这个 404 错误。 Publishing project with the "delete all existing files prior to publish" option saved my problems.使用“发布前删除所有现有文件”选项发布项目解决了我的问题。

hope this helps someone.希望这有助于某人。

I too came across this same issue on an ASP.NET 4.0 Web Forms application.我在 ASP.NET 4.0 Web Forms 应用程序上也遇到了同样的问题。 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.我们采用的解决方案(更适用于 ASP.NET Web 窗体)如下。 Add the following section to Web.Config:将以下部分添加到 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>

Perhaps worth mentioning: I'm running a Web Forms application that uses it's own manual routing.也许值得一提:我正在运行一个 Web 窗体应用程序,它使用它自己的手动路由。 Since OWIN startup occurs after the routing tables have been set, the route for /signalr/hubs was never hit.由于 OWIN 启动发生在路由表设置之后,因此 /signalr/hubs 的路由从未被命中。 I added a rule to ignore routes (IE: let web forms do the routing) on any path that starts with "/signalr".我添加了一条规则来忽略以“/signalr”开头的任何路径上的路由(IE:让 Web 表单进行路由)。 This solved my issue.这解决了我的问题。

我通过将应用程序池 .NET Framework 版本从 v2.0 切换到 v4.0 解决了这个问题。

可能是因为您的集线器类名称是“Test”,而您在客户端指的是“test”。

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. see : 查看生成的代理为您做什么,仅此而已。 Things will work without it.没有它,事情会奏效。

For me the solution was to reinstall all the packages and restore all the dependecies.对我来说,解决方案是重新安装所有软件包并恢复所有依赖项。

Open nuget powershell and use this command.打开 nuget powershell 并使用此命令。

Update-Package -Reinstall

adding app.MapSignalR();添加app.MapSignalR(); in Startup class resolves the issueStartup类中解决了这个问题

In my case (signalr was added to existing, big mvc project) the reason of not firing OWIN Startup.Configruation() were missing "dependedntAssembly" and wrong "targetFramwerork" version in web.config.在我的情况下(signalr 已添加到现有的大型 mvc 项目),不触发 OWIN Startup.Configruation() 的原因在 web.config 中缺少“dependedntAssembly”和错误的“targetFramwerork”版本。

I have created simple signalR project from scratch and compared web.config files.我从头开始创建了简单的 signalR 项目并比较了 web.config 文件。 What has helped us was:对我们有帮助的是:

  1. Correction from 4.5 to 4.5.2 in从 4.5 校正到 4.5.2 in
    <httpRuntime targetFramework="4.5" ... > 
    <compilation debug="true" targetFramework="4.5" ... >
  1. adding dependent assemblies:添加依赖程序集:
<dependentAssembly> 
    <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" culture="neutral" /> 
    <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" /> 
</dependentAssembly>
<dependentAssembly>
    <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" /> 
    <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" /> 
</dependentAssembly>

I was getting that exception because my url hosting was wrong.我收到该异常是因为我的 url 托管错误。 I wrote "/chathub" but in my endpoint It said "/chat"我写了“/chathub”,但在我的端点上写的是“/chat”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM