繁体   English   中英

使用ASP.NET MVC捆绑和缩小时出现奇怪的JavaScript错误

[英]Weird JavaScript error when using ASP.NET MVC bundling and minification

我正在使用ASP.NET MVC 5进行bundling and minification并在我的web.config中将debug设置为false时出现一个奇怪的错误。

我一直在使用捆绑和缩小一段时间,并且昨晚只是第一次看到这个错误,因为我的下拉菜单不起作用。 我无法确认这是否有效,因为我从未检查过文件。

这是部分错误:

/* Minification failed. Returning unminified contents.
(2,1): run-time error CSS1019: Unexpected token, found '!'
(2,2): run-time error CSS1019: Unexpected token, found 'function('
(2,14): run-time error CSS1031: Expected selector, found ')'
(2,14): run-time error CSS1025: Expected comma or open brace, found ')'
(2,212): run-time error CSS1019: Unexpected token, found '('
(2,213): run-time error CSS1019: Unexpected token, found '"undefined"'
(2,224): run-time error CSS1019: Unexpected token, found '!'
(2,225): run-time error CSS1019: Unexpected token, found '='
(2,239): run-time error CSS1031: Expected selector, found '?'
(2,239): run-time error CSS1025: Expected comma or open brace, found '?'
(3): Scanner error CSS1002: Unterminated string: '></a>","#"===a.firstChild.getAttribute("href")})||jb("type|href|height|width",function(a,b,c){return c...

所以我决定创建一个独立的ASP.NET MVC项目,只需使用jQuery 2.1.1 我还是得到了错误。 所以我认为如果我使用jQuery 1.11.1它会消失。 我仍然得到同样的错误。

为什么我会收到此错误? 它显然是jQuery文件中的一部分。 我在Bootstrap的JavaScript文件中遇到了同样的错误。

什么时候最好使用捆绑和缩小? 只有你自己的JavaScript和CSS文件? 你是否应该捆绑和缩小第三方文件,或者只是使用他们的CDN? 我在工作的防火墙后面,所以使用外部CDN对我来说不是一个选择。

这是我的web.config:

<system.web>
    <compilation targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2" />
</system.web>

我的_Layout文件:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>My Website</title>
    </head>
    <body>
        @RenderBody()
        @Scripts.Render("~/bundles/js")
    </body>
</html>

和我的BundleConfig.cs文件:

public class BundleConfig
{
    public static void RegisterBundles(BundleCollection bundles)
    {
        bundles.Add(new StyleBundle("~/bundles/js").Include(
            "~/Scripts/jquery-{version}.js"));
    }
}

产生的输出是:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>My Website</title>
    </head>
    <body>
        <h2>Index</h2>
        <script src="/bundles/js?v=7gm4LP0WuTab97QPOJSizw2PGm8jrBl7HRdnANRLbBY1"></script>
    </body>
</html>

这两个文件也存在问题:

  • html5shiv.js
  • Respond.js

所以问题是,它是无效的JavaScript错误还是捆绑和缩小充满了错误?

有一个问题:

bundles.Add(new StyleBundle("~/bundles/js").Include(
            "~/Scripts/jquery-{version}.js"));

根据js文件使用ScriptBundle而不是StyleBundle

bundles.Add(new ScriptBundle("~/bundles/js").Include(
            "~/Scripts/jquery-{version}.js"
));

您可能StyleBundle项目中的StyleBundle代码复制到普通的MVC项目中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM