我使用ASP.NET MVC 5
和bundling and minification
,并在我的web.config
中将调试设置为false时出现奇怪的错误。使用ASP.NET MVC捆绑和缩小时出现奇怪的JavaScript错误
我一直在使用捆绑和缩小一段时间,并且只是第一次看到这个错误,因为我的下拉菜单没有工作。我无法确认这是否有效,因为我从未检查过这些文件。
下面是部分错误:
/* 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>
它也有这2个文件的问题:
- html5shiv.js
- Respond.js
所以,问题是,它是无效的JavaScript错误或者是它的捆绑和充满缺陷的缩小?
谢谢。我无法相信我错过了这一点。不知道我是如何改变的。你可能是对的,我从别的地方复制粘贴它。 – 2014-12-03 08:23:02
在您的'RegisterBundles'方法附近,您可以创建'RegisterStyles'和'RegisterScripts'方法,从'RegisterBundles'调用它们,我认为这是一个很好的做法。 – Ofiris 2014-12-03 08:28:00