由于MVC试图提供帮助并呈现jquery包,我似乎总是遇到这个问题。然后当你尝试在你的视图中添加signalr时,它会失败,因为你必须事先添加jquery,但是jQuery会被MVC bundle重新加载。这混淆了它似乎会导致错误的东西。你可能都会有这样的节主布局页:
@Scripts.Render("~/bundles/jquery")
@RenderSection("scripts", required: false)
所以,如果你把你的脚本中的脚本段应该做signalr快乐,做工精细。
@section scripts {
<!--Reference the SignalR library. -->
<script src="Scripts/jquery.signalR-2.0.2.min.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>: ' + 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>
}
您能分享您如何解决您的问题吗? – 2013-05-31 21:44:17
此问题通常是由于'〜/ signalr/hubs'处的自动生成的集线器JavaScript代理缺少或无效的脚本引用。 请确保Hub路由在应用程序中的任何其他路由之前注册。 – Yehia 2014-09-27 16:29:13