gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 23:35:48 24 4
gpt4 key购买 nike

这是页面:

        <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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com