2013-07-02 40 views
20

我可以得到这个tutorial工作在一个新的项目,但不是在我现有的项目。signalR:/ signalr/hubs不生成

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

<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>:&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> 

然后,我已经得到了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) { .... 

因为聊天是null作为前行并没有发现chatHub:

var chat = $.connection.chatHub; 

有谁知道什么地方错了我代码?

UPDATE

我已经改变这一行::

<script src="/signalr/hubs"></script> 

<script src="~/signalr/hubs"></script> 

回答

15

解决我的问题我已经解决了我的问题,通过改变线路::

<script src="/signalr/hubs"></script> 

<script src="~/signalr/hubs"></script> 
+12

顺便提一句,'/ signalr/hubs'不是项目中的文件。如果有人想要浪费时间寻找它。如果你启动应用程序,然后去那个网址它会显示给你。 – toddmo

+0

@toddmo,你的意思是它不是一个文件? ,我开发了一个实时通知系统,它不调用$ .connection.hub.start()。我认为〜/ signalr/hubs必须做些什么才能解决问题,即使在添加软件包 – Saurabh

+0

@toddmo后仍然无处可查,因为我不想再发表相同的问题,我的根目录是http:// localhost: 56547/Home/Index,你是否建议去http:// localhost:56547/signalr/hubs? – Saurabh

1

我在那里没有被生成的枢纽文件类似的问题。它看起来像OP是following the steps here。我解决这个问题的方式必须与jquery包含在一起。我在下面链接的教程是用jquery 1.6.4和jquery-signalr version 2.1.0编写的。当Visual Studio为我生成Scripts文件夹时,它使用jquery版本1.10.2和jquery-signalr版本2.0.2。

我解决这个问题的方法只是编辑index.html文件。请注意,您可以使用Chrome的JavaScript控制台窗口Ctrl + Shift + J查看错误。

3

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

public class Startup 
{ 
    public void Configuration(IAppBuilder appBuilder){ 
     ... 
     appBuilder.MapSignalR(); 
     ... 
    } 
... 
2

就我而言,这是因为我的ChatHub类没有被标记为public。

0

我想补充一点,signalR自述文件对这个问题有一些说明。 而且,如果您的signalR页面位于PartialView中,则应将一些脚本放置在母版页中。

Please see http://go.microsoft.com/fwlink/?LinkId=272764 for more information on using SignalR. 

Upgrading from 1.x to 2.0 
------------------------- 
Please see http://go.microsoft.com/fwlink/?LinkId=320578 for more information on how to 
upgrade your SignalR 1.x application to 2.0. 

Mapping the Hubs connection 
---------------------------- 
To enable SignalR in your application, create a class called Startup with the following: 

using Microsoft.Owin; 
using Owin; 
using MyWebApplication; 

namespace MyWebApplication 
{ 
    public class Startup 
    { 
     public void Configuration(IAppBuilder app) 
     { 
      app.MapSignalR(); 
     } 
    } 
} 

Getting Started 
--------------- 
See http://www.asp.net/signalr/overview/getting-started for more information on how to get started. 

Why does ~/signalr/hubs return 404 or Why do I get a JavaScript error: 'myhub is undefined'? 
-------------------------------------------------------------------------------------------- 
This issue is generally due to a missing or invalid script reference to the auto-generated Hub JavaScript proxy at '~/signalr/hubs'. 
Please make sure that the Hub route is registered before any other routes in your application. 

In ASP.NET MVC 4 you can do the following: 

     <script src="~/signalr/hubs"></script> 

If you're writing an ASP.NET MVC 3 application, make sure that you are using Url.Content for your script references: 

    <script src="@Url.Content("~/signalr/hubs")"></script> 

If you're writing a regular ASP.NET application use ResolveClientUrl for your script references or register them via the ScriptManager 
using a app root relative path (starting with a '~/'): 

    <script src='<%: ResolveClientUrl("~/signalr/hubs") %>'></script> 

If the above still doesn't work, you may have an issue with routing and extensionless URLs. To fix this, ensure you have the latest 
patches installed for IIS and ASP.NET. 
0

对我来说,解决办法是重新安装所有和恢复所有依赖条件

打开nuget powershell控制台并使用此命令。

Update-Package -Reinstall