【问题标题】:SignalR not loaded on bundling the js捆绑 js 时未加载 SignalR
【发布时间】:2015-05-12 04:36:52
【问题描述】:

我一直在我的 asp.net 应用程序中使用Bundling and Minification 概念。我在global.asax中有以下代码

void Application_Start(object sender, EventArgs e)
    {
        System.Web.Optimization.BundleTable.Bundles.Add(new System.Web.Optimization.ScriptBundle("~/bundle/js")
                      .Include("~/Scripts/jquery-1.8.2.js", 
                               "~/Scripts/jquery-ui-1.8.24.js",
                               "~/Scripts/jquery.signalR-2.2.0.js"));
        System.Web.Optimization.BundleTable.Bundles.Add(new System.Web.Optimization.ScriptBundle("~/bundle/js1")
                     .Include("~/Content/Scripts/VerbaTrack.js",
                              "~/Content/Scripts/Helper.js"));
        System.Web.Optimization.BundleTable.Bundles.Add(new System.Web.Optimization.StyleBundle("~/bundle/css")
             .Include("~/Content/Css/Demo.css",
                      "~/Content/Css/Styles.css"));
    }

我在master page 中以下列方式引用这些文件

<head runat="server">
<link rel="shortcut icon" type="image/x-icon" href="Content/Images/VerbaTrack.ico"/>
<title>

</title>    
<asp:placeholder runat="server">  
     <%= System.Web.Optimization.Scripts.Render("~/bundle/js") %>
     <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=drawing,places"></script>    
     <%= System.Web.Optimization.Scripts.Render("~/bundle/js1") %>
     <%= System.Web.Optimization.Styles.Render("~/bundle/css") %>   
     <script src="signalr/hubs"></script>               
</asp:placeholder>

如您所见,我在引用 jquery.signalR-2.2.0.js 之后立即引用 signalr/hubs

但它仍然抛出以下错误

Error: SignalR: SignalR is not loaded. Please ensure jquery.signalR-x.js is referenced before ~/signalr/hubs.

为什么会这样?

【问题讨论】:

  • 其他 JS 文件是否像 jQuery 和 jQueryUI JS 文件一样被加载?但只有 SignalR JS 文件没有加载?您是否通过查看页面源检查了这一点?

标签: c# asp.net signalr bundling-and-minification


【解决方案1】:

如果脚本没有加载正确的顺序(请检查页面源代码),您可以使用它来确保顺序与捆绑包中提供的一致:

/// <summary>
/// Respect the order of the scripts added
/// </summary>
public class NonOrderingBundleOrderer : IBundleOrderer
{
    public IEnumerable<BundleFile> OrderFiles(BundleContext context, IEnumerable<BundleFile> files)
    {
        return files;
    }
}

然后将捆绑包分配给如下变量:

var scripts = new ScriptBundle("~/bundle/js")
        .Include("~/Scripts/jquery-1.8.2.js")
        .Include("~/Scripts/jquery-ui-1.8.24.js")
        .Include("/Scripts/jquery.signalR-2.2.0.js");

然后附加NonOrderingBundleOrderer并将捆绑包添加到捆绑包集合中:

scripts.Orderer = new NonOrderingBundleOrderer();
BundleTable.Bundles.Add(scripts);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-12
    • 1970-01-01
    相关资源
    最近更新 更多