MVC が役に立ち、jquery バンドルをレンダリングする方法が原因で、私は常にこの問題に遭遇しているようです。次に、ビューにシグナルを追加しようとすると、事前に jquery を追加する必要があるため失敗しますが、jquery は MVC バンドルによって再度読み込まれます。これにより、見た目が混乱し、エラーが発生します。おそらく、メイン レイアウト ページに次のセクションがあります。
@Scripts.Render("~/bundles/jquery")
@RenderSection("scripts", required: false)
そのため、スクリプトをスクリプト セクションに追加すると、signaler が満足して正常に動作するはずです。
@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>
}