【问题标题】:signalR : /signalr/hubs is not generatedsignalR : 未生成 /signalr/hubs
【发布时间】:2013-07-02 09:29:38
【问题描述】:

我可以让这个tutorial 在新项目中工作,但不能在我现有的项目中工作。

我的项目是一个 ASP.Net MVC 4 Web 应用程序,在 web.config 文件中具有以下属性:

<appSettings>
  <add key="webpages:Enabled" value="true"/>
</appSettings>

这是因为我的应用程序是单页应用程序,它在客户端使用 AngularJS。我的应用程序中唯一的页面是 index.cshtml,我在其中添加了 signalR 的相关代码:

 <!-- signalR chat -->
<script src="~/Scripts/jquery.signalR-1.0.0.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
<script src="/signalr/hubs"></script>
<!--Add script to update the page and send messages.--> 
<script type="text/javascript">
    $(function () {
        // Declare a proxy to reference the hub. 
        var chat = $.connection.chatHub;
        // Create a function that the hub can call to broadcast messages.
        chat.client.broadcastMessage = function (name, message) {
            // Html encode display name and message. 
            var encodedName = $('<div />').text(name).html();
            var encodedMsg = $('<div />').text(message).html();
            // Add the message to the page. 
            $('#discussion').append('<li><strong>' + encodedName
                + '</strong>:&nbsp;&nbsp;' + encodedMsg + '</li>');
        };
        // Get the user name and store it to prepend to messages.
        $('#displayname').val(prompt('Enter your name:', ''));
        // Set initial focus to message input box.  
        $('#message').focus();
        // Start the connection.
        $.connection.hub.start().done(function () {
            $('#sendmessage').click(function () {
                // Call the Send method on the hub. 
                chat.server.send($('#displayname').val(), $('#message').val());
                // Clear text box and reset focus for next comment. 
                $('#message').val('').focus();
            });
        });
    });
</script>

然后我得到了 ChatHub.cs 文件:

public class ChatHub : Hub
{
    public void Send(string name, string message)
    {
        // Call the broadcastMessage method to update clients.
        Clients.All.broadcastMessage(name, message);
    }
}

最后在 global.asax 中:

 protected void Application_Start()
    {
        RouteTable.Routes.MapHubs();
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }

当我运行应用程序时,没有生成 /signalr/hubs 文件。我在请求文件时收到 404,它在线上崩溃:

 chat.client.broadcastMessage = function (name, message) { ....

因为上一行没有找到chatHub,所以聊天为空:

var chat = $.connection.chatHub;

有人知道我的代码有什么问题吗?

更新

我通过换行解决了我的问题::

<script src="/signalr/hubs"></script>

<script src="~/signalr/hubs"></script>

【问题讨论】:

    标签: asp.net-mvc-4 angularjs signalr single-page-application


    【解决方案1】:

    我通过换行解决了我的问题::

    <script src="/signalr/hubs"></script>
    

    <script src="~/signalr/hubs"></script>
    

    【讨论】:

    • 顺便说一句,/signalr/hubs 不是项目中的文件。以防有人想浪费时间寻找它。如果您启动该应用程序,然后转到该网址,它将显示给您。
    • @toddmo ,你的意思是它不是一个文件? ,我开发了一个实时通知系统,它不调用 $.connection.hub.start() 。我认为 ~/signalr/hubs 必须对它做点什么,即使在添加包后它也无法解决
    • @toddmo,抱歉发表评论,因为我不想再发表同样的问题,我的根目录是localhost:56547/Home/Index,你建议去localhost:56547/signalr/hubs 吗??
    • @toddmo 是的,是的
    • @toddmo,我尝试检查 url (localhost:50877/signalr/hubs)。它给了我资源未找到错误页面。你能帮我吗。因此,我认为我的“ var chat = $.connection.chatHub; ”返回一个空值。你能帮帮我吗?
    【解决方案2】:

    另外,没有生成 /signalr/hubs 的原因是忘记在 OWIN 启动配置中映射 SignalR。

    public class Startup
    {
       public void Configuration(IAppBuilder appBuilder){
             ...
             appBuilder.MapSignalR();
             ...
       }
     ...
    

    【讨论】:

      【解决方案3】:

      就我而言,这是因为我的 ChatHub 课程未标记为公开。

      【讨论】:

        【解决方案4】:

        我遇到了一个类似的问题,即未生成集线器文件。看起来 OP 是following the steps here。我解决问题的方式与 jquery 包括有关。我在下面链接的教程是用 jquery 1.6.4 和 jquery-signalr 版本 2.1.0 编写的。当 Visual Studio 为我生成 Scripts 文件夹时,它使用 jquery 版本 1.10.2 和 jquery-signalr 版本 2.0.2。

        我解决这个问题的方法只是编辑 index.html 文件。请注意,您可以使用 Chrome 的 javascript 控制台窗口 Ctrl+Shift+J 查看错误。

        【讨论】:

          【解决方案5】:

          对我来说,解决方案是重新安装所有软件包并恢复所有依赖项

          打开 nuget powershell 控制台并使用此命令。

          Update-Package -Reinstall
          

          【讨论】:

          • 谢谢 - 这就是我要找的东西! OP 是关于由于设置不正确/不完整而尚未起作用的场景....但是如果那些 signalR/hubs 请求开始突然返回 404 - 这个答案可能对你有用!明显症状:适用于其他开发机器,但不适用于您的机器。
          【解决方案6】:

          我想补充一点,signalR 自述文件对此问题有一些说明。 此外,如果您的 signalR 页面位于 PartialView 中,则应将一些脚本放置在母版页中。

          Please see http://go.microsoft.com/fwlink/?LinkId=272764 for more information on using SignalR.
          
          Upgrading from 1.x to 2.0
          -------------------------
          Please see http://go.microsoft.com/fwlink/?LinkId=320578 for more information on how to 
          upgrade your SignalR 1.x application to 2.0.
          
          Mapping the Hubs connection
          ----------------------------
          To enable SignalR in your application, create a class called Startup with the following:
          
          using Microsoft.Owin;
          using Owin;
          using MyWebApplication;
          
          namespace MyWebApplication
          {
              public class Startup
              {
                  public void Configuration(IAppBuilder app)
                  {
                      app.MapSignalR();
                  }
              }
          } 
          
          Getting Started
          ---------------
          See http://www.asp.net/signalr/overview/getting-started for more information on how to get started.
          
          Why does ~/signalr/hubs return 404 or Why do I get a JavaScript error: 'myhub is undefined'?
          --------------------------------------------------------------------------------------------
          This issue is generally due to a missing or invalid script reference to the auto-generated Hub JavaScript proxy at '~/signalr/hubs'.
          Please make sure that the Hub route is registered before any other routes in your application.
          
          In ASP.NET MVC 4 you can do the following:
          
                <script src="~/signalr/hubs"></script>
          
          If you're writing an ASP.NET MVC 3 application, make sure that you are using Url.Content for your script references:
          
              <script src="@Url.Content("~/signalr/hubs")"></script>
          
          If you're writing a regular ASP.NET application use ResolveClientUrl for your script references or register them via the ScriptManager 
          using a app root relative path (starting with a '~/'):
          
              <script src='<%: ResolveClientUrl("~/signalr/hubs") %>'></script>
          
          If the above still doesn't work, you may have an issue with routing and extensionless URLs. To fix this, ensure you have the latest 
          patches installed for IIS and ASP.NET. 
          

          【讨论】:

            【解决方案7】:

            就我而言,我失踪了:

                    app.MapSignalR();
            

            在位于startup.cs的public void Configuration(IAppBuilder app)函数中

            【讨论】:

              【解决方案8】:

              对于那些从一开始就开始向外扩展的人的一些建议。就我而言,我正在努力让远程客户端工作,但从未意识到

              A.教程示例在 using 语句中列出了 web 应用程序(服务器)启动,之后 web 应用程序正确处理并且不再存在。

              我去掉了 using 语句并保留了对 web 应用程序的引用 供以后处理

              B.客户端的 url 与服务器不同。该示例依赖于它们具有相同的 url。 “/signalr/hubs”是由信号器服务器运行的端点,由信号器客户端调用以获取服务器实现的所有集线器的脚本。

              您需要包含“http://myServerURL/signalr/hubs”,而不是添加 相对于客户。

              没有谎言。这让我绊倒了整整 2 周,因为通过某种魔法,该解决方案无论如何都适用于我同事的设置。这导致我一直在寻找 IIS 设置和防火墙设置以及 CORS 设置,这些设置一定会阻止我的计算机上的连接。我尽可能地清理了最后一个堆栈溢出问题,最终的答案是我应该从一开始就在 Web 应用程序上实现心跳监视器。

              祝你好运,希望这可以节省其他人一些时间。

              【讨论】:

              • 为什么我被否决了?这个答案解释了 asp.net 实现需要额外工作的真正原因,并且适用于 Signalr 的所有用途,而不是仅发布者的实现。
              猜你喜欢
              • 1970-01-01
              • 2013-07-31
              • 2012-06-24
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2019-12-03
              相关资源
              最近更新 更多