signalR:/ signalr/hubs未生成

Sam*_*Sam 23 asp.net-mvc-4 signalr angularjs single-page-application

我可以让本教程在新项目中工作,但不能在我现有的项目中工作.

我的项目是一个ASP.Net MVC 4 Web应用程序,在web.config文件中具有以下属性:

<appSettings>
  <add key="webpages:Enabled" value="true"/>
</appSettings>
Run Code Online (Sandbox Code Playgroud)

这是因为我的应用程序是单页面应用程序,它在客户端使用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>:&nbsp;&nbsp;' + 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>
Run Code Online (Sandbox Code Playgroud)

然后我有了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);
    }
}
Run Code Online (Sandbox Code Playgroud)

最后在global.asax中:

 protected void Application_Start()
    {
        RouteTable.Routes.MapHubs();
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
Run Code Online (Sandbox Code Playgroud)

当我运行应用程序时,不会生成/ signalr/hubs文件.我在请求文件时得到404,它崩溃了:

 chat.client.broadcastMessage = function (name, message) { ....
Run Code Online (Sandbox Code Playgroud)

因为聊天是空的,因为上一行找不到chatHub:

var chat = $.connection.chatHub;
Run Code Online (Sandbox Code Playgroud)

有谁知道我的代码有什么问题?

UPDATE

我通过更改行解决了我的问题::

<script src="/signalr/hubs"></script>
Run Code Online (Sandbox Code Playgroud)

<script src="~/signalr/hubs"></script>
Run Code Online (Sandbox Code Playgroud)

Sam*_*Sam 18

我通过更改行解决了我的问题::

<script src="/signalr/hubs"></script>
Run Code Online (Sandbox Code Playgroud)

<script src="~/signalr/hubs"></script>
Run Code Online (Sandbox Code Playgroud)

  • BTW,`/ signalr/hubs`不是项目中的文件.如果有人想浪费时间寻找它.如果您启动该应用程序,然后转到该网址,它将显示给您. (15认同)

Mar*_*bad 8

此外,未生成/ signalr/hubs的原因是忘记在OWIN启动配置中映射SignalR.

public class Startup
{
   public void Configuration(IAppBuilder appBuilder){
         ...
         appBuilder.MapSignalR();
         ...
   }
 ...
Run Code Online (Sandbox Code Playgroud)