使用捆绑优化在IIS上发布我的ASP.NET MVC应用程序时,为什么没有正确加载SignalR?

时间:2017-10-24 17:08:21

标签: c# asp.net iis signalr

似乎在使用带有IIS的开发服务器上使用SignalR运行我的应用程序时,我在JS控制台中得到以下错误(虽然它在Debug中本地运行应用程序完全正常):

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

第17行与:

有关
if (typeof ($.signalR) !== "function") {
    throw new Error("SignalR: SignalR is not loaded. Please ensure jquery.signalR-x.js is referenced before ~/signalr/js.");
}

106号线就是IIFE的终点。

以下是BundleConfig类:

public class BundleConfig
{
    public static void RegisterBundles(BundleCollection bundles)
    {
        var scriptBundle = new ScriptBundle("~/Scripts/bundle");

        // jQuery
        scriptBundle.Include("~/Scripts/jquery-3.1.1.min.js");

        // Bootstrap
        scriptBundle.Include("~/Scripts/bootstrap.min.js");

        // Knockout
        scriptBundle.Include("~/Scripts/knockout-{version}.js");

        // SignalR
        scriptBundle.Include("~/Scripts/jquery.signalR-{version}.js");

        // Custom site scripts
        scriptBundle.Include("~/Scripts/scripts.js");
        scriptBundle.Include("~/Scripts/scripts-bootstrap.js");
        scriptBundle.Include("~/Scripts/scripts-devextreme.js");


        var styleBundle = new StyleBundle("~/Content/bundle");

        // Bootstrap
        styleBundle.Include("~/Content/bootstrap-telus-theme.min.css");

        // Font Awesome
        styleBundle.Include("~/Content/font-awesome.min.css");

        // Custom site styles
        styleBundle.Include("~/Content/Site.css");
        styleBundle.Include("~/Content/Site-bootstrap-plus.css");
        styleBundle.Include("~/Content/Site-devextreme-plus.css");


        bundles.Add(scriptBundle);
        bundles.Add(styleBundle);

        #if !DEBUG
        BundleTable.EnableOptimizations = true;
        #endif
    }
}

开发服务器上的doctype如下所示:

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>Login</title>
    <link rel="favicon" href="/Content/favicons/favicon.ico" />
    <link rel="favicon" sizes="32x32" href="/Content/favicons/favicon-32.png" />
    <link rel="favicon" sizes="128x128" href="/Content/favicons/favicon-128.png" />
    <link rel="apple-touch-icon" href="/Content/favicons/apple-touch-icon.png" />
    <link rel="apple-touch-icon" sizes="72x72" href="/Content/favicons/apple-touch-icon-72.png" />
    <link rel="apple-touch-icon" sizes="114x114" href="/Content/favicons/apple-touch-icon-114.png" />
    <link rel="apple-touch-icon" sizes="144x144" href="/Content/favicons/apple-touch-icon-144.png" />

    <link href="/Content/bundle?v=KoFrSDNrdsCobjey_0WoDyRHewQDRrB1Ov9eEHg15EI1" rel="stylesheet"/>

    <link href="/Content/DevExtremeBundle?v=dvjWkuzRNPhpuO8epzd1bapDA4VbH3NGrM9XfK6lfH41" rel="stylesheet"/>

    <script src="/Scripts/bundle?v=bk8qAjn5FOZNS6DjmQ8qRN1l5sZzYFEHYS7tNOnf4vk1"></script>

    <script src="/Scripts/DevExtremeBundle?v=EDbcZoJEzBcOEMDXw50S76TV4vRJy8nCjafsccwoAas1"></script>

    <script src="/signalr/hubs" type="text/javascript"></script>
</head>

在本地调试中(所以没有捆绑优化):

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>Login</title>
    <link rel="favicon" href="/Content/favicons/favicon.ico" />
    <link rel="favicon" sizes="32x32" href="/Content/favicons/favicon-32.png" />
    <link rel="favicon" sizes="128x128" href="/Content/favicons/favicon-128.png" />
    <link rel="apple-touch-icon" href="/Content/favicons/apple-touch-icon.png" />
    <link rel="apple-touch-icon" sizes="72x72" href="/Content/favicons/apple-touch-icon-72.png" />
    <link rel="apple-touch-icon" sizes="114x114" href="/Content/favicons/apple-touch-icon-114.png" />
    <link rel="apple-touch-icon" sizes="144x144" href="/Content/favicons/apple-touch-icon-144.png" />

    <link href="/Content/bootstrap-telus-theme.min.css" rel="stylesheet"/>
<link href="/Content/font-awesome.min.css" rel="stylesheet"/>
<link href="/Content/Site.css" rel="stylesheet"/>
<link href="/Content/Site-bootstrap-plus.css" rel="stylesheet"/>
<link href="/Content/Site-devextreme-plus.css" rel="stylesheet"/>

    <link href="/Content/dx.common.css" rel="stylesheet"/>
<link href="/Content/dx.telus.light.css" rel="stylesheet"/>

    <script src="/Scripts/jquery-3.1.1.min.js"></script>
<script src="/Scripts/bootstrap.min.js"></script>
<script src="/Scripts/knockout-3.4.2.debug.js"></script>
<script src="/Scripts/jquery.signalR-2.2.2.js"></script>
<script src="/Scripts/scripts.js"></script>
<script src="/Scripts/scripts-bootstrap.js"></script>
<script src="/Scripts/scripts-devextreme.js"></script>

    <script src="/Scripts/cldr.js"></script>
<script src="/Scripts/cldr/event.js"></script>
<script src="/Scripts/cldr/supplemental.js"></script>
<script src="/Scripts/cldr/unresolved.js"></script>
<script src="/Scripts/globalize.js"></script>
<script src="/Scripts/globalize/message.js"></script>
<script src="/Scripts/globalize/number.js"></script>
<script src="/Scripts/globalize/currency.js"></script>
<script src="/Scripts/globalize/date.js"></script>
<script src="/Scripts/jszip.js"></script>
<script src="/Scripts/dx.all.debug.js"></script>
<script src="/Scripts/aspnet/dx.aspnet.data.js"></script>
<script src="/Scripts/aspnet/dx.aspnet.mvc.js"></script>

    <script src="/signalr/hubs" type="text/javascript"></script>
</head>

在开发服务器上发布的内容不能使用DEBUG常量运行,如何在没有它的情况下使其工作? Imho BundleTable.EnableOptimizations = true;线不应该弄乱我的信号捆绑,但这仍然是我在两者之间看到的唯一区别。但是,在本地运行开发服务器配置时,即使启用了优化捆绑包,它也可以正常运行...

1 个答案:

答案 0 :(得分:1)

问题出现了,因为捆绑优化正在跳过最小文件。你捆绑的JS没有jQuery,这是SignalR所必需的。

有关详细信息,请查看以下答案:Bundler not including .min files