asp.net-mvc - SignalR 2 不生成/signalr/hubs

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

这是页面:

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

启动.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 未找到错误,当我尝试运行页面时,我得到:

 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但它不起作用。

有什么想法吗?

最佳答案

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

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

关于asp.net-mvc - SignalR 2 不生成/signalr/hubs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26273766/

相关文章:

javascript - 我应该如何使用 SignalR 创建 "screen sharing"

css - 两次导入相同的 less 文件时出现 Dotless MVC 捆绑问题

asp.net-mvc - ASP.NET MVC 应用程序自定义错误页面未显示在共享托管环境中

asp.net-mvc - ASP.Net MVC 的 RESX 文件编辑器

c# - 在 ASP.Net 中使用的最佳 session 状态模式是什么,这样我的网站的 CPU 使用率就不会很高?

c# - 使用 Azure SignalR 服务和 Azure Functions 进行本地开发

c# - SignalR OnConnected 与多个服务器和 Redis 背板

javascript - 将 Vue.js 集成到现有 ASP.NET MVC5 项目的最佳方式

asp.net - 使用 SQL Server 将信号发送者用户映射到连接

javascript - SignalR Client 开始连接时如何设置用户?