signalR:/ signalr / hubs未生成

时间:2013-07-02 09:29:39

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

我可以让这个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>

8 个答案:

答案 0 :(得分:16)

我通过更改行::

解决了我的问题
<script src="/signalr/hubs"></script>

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

答案 1 :(得分:6)

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

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

答案 2 :(得分:2)

就我而言,这是因为我的ChatHub类没有标记为公开。

答案 3 :(得分:1)

我遇到了类似的问题,即没有生成集线器文件。看起来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查看错误。

答案 4 :(得分:0)

我想补充一点,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. 

答案 5 :(得分:0)

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

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

Update-Package -Reinstall

答案 6 :(得分:0)

就我而言,我不见了:

obj[k]

位于startup.cs中的 app.MapSignalR(); 函数中

答案 7 :(得分:-1)

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

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

我摆脱了using语句,并保留了对该Web应用程序的引用 供以后处理

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

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

没有谎言。这使我绊倒了2个星期,因为通过某种魔术,该解决方案无论如何在我同事的设置中仍然有效。这使我一直在寻找必须阻止计算机上的连接的IIS设置,防火墙设置和CORS设置。我尽力解决了每个堆栈溢出问题,最终的答案是我应该从一开始就在Web应用程序上实现心跳监视器。

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