【问题标题】:SignalR 2 does not generate /signalr/hubsSignalR 2 不生成 /signalr/hubs
【发布时间】:2014-10-09 08:30:35
【问题描述】:

这是页面:

        <script src="~/Scripts/jquery-1.10.2.min.js"></script>
    <script src="~/Scripts/jquery.signalR-2.1.2.min.js"></script>
    <!--Reference the autogenerated SignalR hub script. -->
    <script src="~/signalr/hubs"></script>
    <!--SignalR script to update the chat page and send messages.-->
    <script>
        $(function () {
            // Reference the auto-generated proxy for the hub.
            var notification = $.connection.notificationHub;
            // Create a function that the hub can call back to display messages.
            notification.client.addNewMessage = function (message) {
                // Add the message to the page.
                $('#discussion').append('<li><strong>'
                    + '</strong>: ' + htmlEncode(message) + '</li>');
            };
            // 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($('#message').val());
                    // Clear text box and reset focus for next comment.
                    $('#message').val('').focus();
                });
            });
        });
        // This optional function html-encodes messages for display in the page.
        function htmlEncode(value) {
            var encodedValue = $('<div />').text(value).html();
            return encodedValue;
        }
    </script>

这里是集线器类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;

namespace AdminWebApp.Hubs
{
     [HubName("notificationHub")] 
    public class NotificationHub : Hub
    {

        public void SendNotification(string message)
        {
            Clients.All.addNewMessage(message);
        }
    }
}

Startup.cs:

using Microsoft.Owin;
using Owin;

[assembly: OwinStartupAttribute(typeof(AdminWebApp.Startup))]
namespace AdminWebApp
{
    public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            ConfigureAuth(app);
        }
    }
}

当我尝试访问时:http://localhost:4551/signalr/hubs 我收到 HTTP 404 not found 错误,当我尝试运行该页面时,我收到:

 Failed to load resource: the server responded with a status of 404 (Not Found)
 Uncaught TypeError: Cannot read property 'client' of undefined 

我试过这个:signalR : /signalr/hubs is not generated,但没用。

有什么想法吗?

【问题讨论】:

    标签: asp.net-mvc signalr signalr-hub signalr.client


    【解决方案1】:

    在 Application_Start 事件的 Global.asax 文件中,您必须注册中心 url。

        protected void Application_Start()
        {
            RouteTable.Routes.MapHubs();
        }
    

    【讨论】:

    • 这仅在 SignalR 1 中是必需的,并且在 SignalR 2 中给出错误:asp.net/signalr/overview/releases/…
    • 这一行缺少 app.MapSignalR();
    • 添加:app.MapSignalR();到 Startup.cs 修复它 Aravind Sivam。谢谢
    • 这是旧方法 - 如果使用 [OwinStartupAttribute],则此方法已过时
    【解决方案2】:

    它对我有用:

    转到项目中的启动类,在 Configuration 方法中添加:

    app.MapSignalR("/signalr", new HubConfiguration());

    希望对你有用

    【讨论】:

    • 在探索完以上所有内容之后,这对我有用!谢谢!
    【解决方案3】:

    试试这个,无需在 App-Start Event 中写一行

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using Owin;
    using Microsoft.Owin;
    [assembly: OwinStartup(typeof(Faceless_Books.Hubs.Startup))]
    
    namespace Faceless_Books.Hubs
    {
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            // Any connection or hub wire up and configuration should go here
            app.MapSignalR();
        }
    }
    }
    

    【讨论】:

      【解决方案4】:

      如果您使用的是 Visual Studio 2012,则 SignalR Hub 类 (v2) 模板将不可用。您可以添加一个名为 ChatHub 的普通类。

      要遵循的步骤

      1) In Solution Explorer, right-click the project, select **Add** | SignalR Hub Class (v2). Name the class ChatHub.cs

      2)修改类

      using System;
      using System.Web;
      using Microsoft.AspNet.SignalR;
      namespace SignalRChat
      {
          public class ChatHub : Hub
          {
              public void Send(string name, string message)
              {
                  // Call the broadcastMessage method to update clients.
                  Clients.All.broadcastMessage(name, message);
              }
          }
      }
      

      【讨论】:

        【解决方案5】:

        在我的示例中,我从示例中复制/粘贴了类的名称,但这不是我的类的名称。

        JS:

        var notification = $.connection.notificationHub; // <--- my class is not called "NotificationHub"
        

        C#:

        public class ToastHub : Hub  // <--- should have been $.connection.toastHub above
        { ... }
        

        【讨论】:

          【解决方案6】:

          您可能需要禁用请求过滤才能启用拉集线器脚本

          <system.webServer>
              <security>
                <requestFiltering>
                  <hiddenSegments>
                    <clear></clear>
                  </hiddenSegments>
                </requestFiltering>        
              </security>
          </system.webServer>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-07-31
            • 2012-06-24
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-09-29
            • 2019-12-03
            • 1970-01-01
            相关资源
            最近更新 更多