无论我尝试什么修复方法,我的 JavaScript 中都会出现这个错误。即使我的 _layout 中有所有 SignalR JavaScript 库,就好像 $.connection 没有被识别。我在 Chrome 浏览器控制台中收到以下错误:Uncaught TypeError: "Cannot read property 'multipleFileHub' of undefined Index:508 (anonymous function) Index:508 x.event.dispatch jquery-2.0.2.js:4692 y.handle jquery-2.0.2.js:4376“未定义”。
我的 Global.asax 继承自“StsMvcHttpApplication”而不是标准的“System.Web.HttpApplication”是否重要?就我而言,我必须输入“RouteTable.Routes.MapHubs();” 在我的“RegisterRoutes”方法而不是“Application_Start”中,因为 Application_Start 启动速度不够快......如果我将它放在应用程序启动中,它就会开始寻找控制器。
将不胜感激!我将首先显示布局代码,然后显示所有单独的代码:
_布局
@section head
{
@Scripts.Render("~/Scripts/Libs/jquery-2.0.2.min.js")
@Scripts.Render("~/Scripts/Libs/jquery-ui-1.10.3.min.js")
@Scripts.Render("~/Scripts/Libs/jquery.validate.min.js")
@Scripts.Render("~/Scripts/Libs/jquery.validate.unobtrusive.min.js")
@Scripts.Render("~/Scripts/Libs/modernizr-2.6.2.js")
@Scripts.Render("~/Scripts/Libs/modernizr.custom.blobconstructor.js")
@Scripts.Render("~/Scripts/SidebarMenu.js")
@Scripts.Render("~/Scripts/BC_Common.js")
@Scripts.Render("~/Scripts/scene.layoutservice.js")
@Scripts.Render("~/Scripts/scene.dataservice.js")
@Scripts.Render("~/Scripts/jquery.signalR-1.1.2.min.js")
@Scripts.Render("~/signalr/hubs")
@Scripts.Render("~/Scripts/scene.startup.js")
}
索引.CSHTML
$('#dBtn').click(function () {
var docIds = sceneLayoutService.getSelection();
if (docIds.length === 0) {
alert("you need to select one");
return false;
} else {
var docIdsParam = jQuery.param(docIds.map(function (value) {
return { "name": "docIds", "value": value };
}));
// Proxy created on the fly
var test_connection = $.connection.multipleFileHub;
// Start the connection
$.connection.hub.start().done(function() {
test_connection.server.send("test");
});
}
return true;
});
服务器代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
namespace Portal.Web.Hubs
{
[HubName("multipleFileHub")]
public class multipleFileHub : Hub
{
public void Send(string message)
{
// Call the addMessage method on all clients
Clients.All.addMessage(message);
}
}
}
全球.ASAX 路由
public static void RegisterRoutes(RouteCollection routes)
{
RouteTable.Routes.MapHubs();
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });
routes.Ignore("{*allpng}", new { allpng = @".*\.png(/.*)?" });
routes.Ignore("{*allgif}", new { allgif = @".*\.gif(/.*)?" });
routes.Ignore("{*alljpg}", new { alljpg = @".*\.jpg(/.*)?" });
routes.MapRoute(
"Error", // Route name
"Error/{action}/{id}", // URL with parameters
new {controller = "Error", action = "Index", id = UrlParameter.Optional });
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Landing", id = UrlParameter.Optional } // Parameter defaults
);
}
页面上的所有 JAVASCRIPT 参考
<script src="/ConnectPortal/Scripts/Libs/jquery-2.0.2.min.js"></script>
<script src="/ConnectPortal/Scripts/Libs/jquery-ui-1.10.3.min.js"></script>
<script src="/ConnectPortal/Scripts/Libs/jquery.validate.min.js"></script>
<script src="/ConnectPortal/Scripts/Libs/jquery.validate.unobtrusive.min.js"></script>
<script src="/ConnectPortal/Scripts/Libs/modernizr-2.6.2.js"></script>
<script src="/ConnectPortal/Scripts/Libs/modernizr.custom.blobconstructor.js"></script>
<script src="/ConnectPortal/Scripts/SidebarMenu.js"></script>
<script src="/ConnectPortal/Scripts/BC_Common.js"></script>
<script src="/ConnectPortal/Scripts/scene.layoutservice.js"></script>
<script src="/ConnectPortal/Scripts/scene.dataservice.js"></script>
<script src="/ConnectPortal/Scripts/jquery.signalR-1.1.2.min.js"></script>
<script src="/ConnectPortal/signalr/hubs"></script>
<script src="/ConnectPortal/Scripts/scene.startup.js"></script>