signalR:/ signalr / hubs未生成

问题描述 投票:22回答:7

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

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

UPDATE

我通过更改行解决了我的问题::

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

<script src="~/signalr/hubs"></script>
asp.net-mvc-4 angularjs signalr single-page-application
7个回答
16
投票

我通过更改行解决了我的问题::

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

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

7
投票

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

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

2
投票

在我的情况下,这是因为我的ChatHub类没有标记为public。


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查看错误。


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. 

0
投票

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

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

Update-Package -Reinstall

0
投票

在我的情况下,我失踪了:

        app.MapSignalR();

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

© www.soinside.com 2019 - 2024. All rights reserved.