Я могу получить tutorial для работы в новом проекте, но не в моем существующем проекте.
Мой проект - это веб-приложение ASP.Net MVC 4 со следующим атрибутом в файле web.config:
<appSettings>
<add key="webpages:Enabled" value="true"/>
</appSettings>
Это связано с тем, что мое приложение представляет собой одностраничное приложение, использующее AngularJS на стороне клиента. Единственной страницей в моем приложении является index.cshtml, к которой я добавил соответствующий код для signalR:
<!-- signalR chat -->
<script src="~/Scripts/jquery.signalR-1.0.0.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>
Затем у меня есть файл ChatHub.cs:
public class ChatHub : Hub
{
public void Send(string name, string message)
{
// Call the broadcastMessage method to update clients.
Clients.All.broadcastMessage(name, message);
}
}
И наконец, в global.asax:
protected void Application_Start()
{
RouteTable.Routes.MapHubs();
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
Когда я запускаю приложение, файл /signalr/hubs не генерируется. Я получаю 404 при запросе файла, и он выходит из строя на линии:
chat.client.broadcastMessage = function (name, message) { ....
потому что chat имеет значение null, поскольку предыдущая строка не нашла chatHub:
var chat = $.connection.chatHub;
Кто-нибудь знает, что случилось с моим кодом?
UPDATE
Я решил свою проблему, изменив строку::
<script src="/signalr/hubs"></script>
to
<script src="~/signalr/hubs"></script>