C# SignalR“signalr/hubs”给出 404 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8941595/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:19:20  来源:igfitidea点击:

SignalR "signalr/hubs" giving 404 error

c#asp.netsignalr

提问by Rocky Singh

I am using SignalR(https://github.com/SignalR/SignalR) in my project. From here https://github.com/SignalR/SignalR/wiki/QuickStart-HubsI 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/hubsgiving 404 error

我在我的项目中使用 SignalR(https://github.com/SignalR/SignalR)。从这里https://github.com/SignalR/SignalR/wiki/QuickStart-Hubs我得到了如何使用集线器的想法。但是“signalr/hubs”脚本给出了 404 错误。这是在查看源中变为的 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">
....

采纳答案by MatteKarla

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.

可能是您没有添加对SignalR.AspNet.dll. 如果我没记错的话,它负责将路线添加到/signalr/hubs.

回答by thomaux

You need to reference the JavaScript file with @Url.Content, assuming ou're using ASP.NET MVC3

您需要使用 引用 JavaScript 文件@Url.Content,假设您使用的是 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.Contentyourself if you're using MVC4 or later. Simply prepending the uri with ~/suffices. For more information, check out this other SO question.

编辑:正如 Trevor De Koekkoek 在他们的评论中提到的,@Url.Content如果您使用的是 MVC4 或更高版本,则不需要添加自己。简单地在 uri 前面加上~/就足够了。有关更多信息,请查看其他 SO 问题

回答by SecretDeveloper

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 服务器时,它起作用了。

enter image description here

enter image description here

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.

David Fowler 评论了针对此问题提出的缺陷。该问题将在未来版本中修复,但现在这是解决方法。我目前找不到该错误的链接。

回答by Michael R

From the SignalR 1.0.0 RC2there is a README in the packages folder that says the Hubs route must be manually established. :) Here is a snippet...

SignalR 1.0.0 RC2 开始,包文件夹中有一个自述文件,说明必须手动建立集线器路由。:) 这是一个片段...

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();
        }
    }
}

回答by Vinix

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

我在 1.0.0 RC2 上遇到了同样的问题,我按照 Michael 的建议做了,而且效果很好。谢谢。

@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.Owin
  • Microsoft.AspNet.SignalR.SystemWeb
  • Microsoft.Owin.Host.SystemWeb
  • 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:

我必须手动添加 Microsoft.CSharp 否则在编译过程中会出现以下错误:

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

回答by Adamy

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);

回答by Dunken

Have you just installed IIS? In this case you might have to reinstall it:

你刚刚安装了IIS吗?在这种情况下,您可能需要重新安装它:

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

回答by arun.m

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.

当我更新到 Signalr 2.0 并将 MVC 项目部署到生产服务器时,我遇到了这个 404 错误。使用“发布前删除所有现有文件”选项发布项目解决了我的问题。

hope this helps someone.

希望这有助于某人。

回答by Saad

This is a full answer from SignalR wiki (https://github.com/SignalR/SignalR/wiki/Faq). It worked with me:

这是 SignalR wiki ( https://github.com/SignalR/SignalR/wiki/Faq)的完整答案。它和我一起工作:

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>

回答by Leniel Maccaferri

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

长话短说:就我而言,我使用了R# (ReSharper),右键单击我使用 SignalR 的项目,然后单击 Refactor => Remove Unused References。当心:这是脚中射击。:D

It removed IMPORTANT DLL references from SignalRthat are used only during runtime, that is, they are not necessary during compile time. Obviously R# only checks for compile time dependencies.

它删除了SignalR仅在运行时使用的重要 DLL 引用,也就是说,它们在编译时不是必需的。显然 R# 只检查编译时的依赖关系。

Namely these 2 references were missing:

即缺少这 2 个参考文献:

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

Removed the NuGet packages and then reinstalled them. Fixed the /hubs404 issue.

删除 NuGet 包,然后重新安装它们。修复了/hubs404 问题。