C# 如果 EnableOptimization 设置为 true,则 JS 包不会呈现
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13458642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JS bundles do not render without EnableOptimization set to true
提问by Stan
I don't know if I'm doing something wrong but it's probably a bug inside MVC4. I wonder how can I fix this?
我不知道我是否做错了什么,但这可能是 MVC4 中的一个错误。我想知道我该如何解决这个问题?
Working scenario
工作场景
public class BundleConfig
{
public static void RegisterBundles(BundleCollection bundles)
{
ScriptBundle scriptBundle = new ScriptBundle("~/js");
string[] scriptArray =
{
"~/content/plugins/jquery/jquery-1.8.2.min.js",
"~/content/plugins/jquery/jquery-ui-1.9.0.min.js",
"~/content/plugins/jquery/jquery.validate.min.js",
"~/content/plugins/jquery/jquery.validate.unobtrusive.min.js",
"~/content/plugins/bootstrap/js/bootstrap.min.js",
};
scriptBundle.Include(scriptArray);
scriptBundle.IncludeDirectory("~/content/js", "*.js");
bundles.Add(scriptBundle);
BundleTable.EnableOptimizations = true;
}
}
@Scripts.Render("~/js")
Converts to (e.g. IT WORKS!)<script src="/js?v=VeCPNK561DZp34yjmWbLrNM35Kf6gaNDl0xsMMC25BQ1"></script>
@Scripts.Render("~/js")
转换为(例如 IT WORKS!)<script src="/js?v=VeCPNK561DZp34yjmWbLrNM35Kf6gaNDl0xsMMC25BQ1"></script>
Not so much working scenario
没有那么多工作场景
public class BundleConfig
{
public static void RegisterBundles(BundleCollection bundles)
{
ScriptBundle scriptBundle = new ScriptBundle("~/js");
string[] scriptArray =
{
"~/content/plugins/jquery/jquery-1.8.2.min.js",
"~/content/plugins/jquery/jquery-ui-1.9.0.min.js",
"~/content/plugins/jquery/jquery.validate.min.js",
"~/content/plugins/jquery/jquery.validate.unobtrusive.min.js",
"~/content/plugins/bootstrap/js/bootstrap.min.js",
};
scriptBundle.Include(scriptArray);
scriptBundle.IncludeDirectory("~/content/js", "*.js");
bundles.Add(scriptBundle);
// BundleTable.EnableOptimizations = true; // I could set it to 'false' for same result, it's false by default
}
}
@Scripts.Render("~/js")
Converts to (e.g. IT DOES NOT WORK!)(nothing, couple of empty break lines)
@Scripts.Render("~/js")
转换为(例如它不起作用!)(nothing, couple of empty break lines)
采纳答案by Fenton
If my understanding is correct, you should define your bundles using the "non-min" JavaScript files. When you enable optimizations it will swap the non-min files for the min files for you:
如果我的理解是正确的,您应该使用“非最小”JavaScript 文件定义您的包。当您启用优化时,它将为您交换非最小文件为最小文件:
public class BundleConfig
{
public static void RegisterBundles(BundleCollection bundles)
{
ScriptBundle scriptBundle = new ScriptBundle("~/js");
string[] scriptArray =
{
"~/content/plugins/jquery/jquery-1.8.2.js",
"~/content/plugins/jquery/jquery-ui-1.9.0.js",
"~/content/plugins/jquery/jquery.validate.js",
"~/content/plugins/jquery/jquery.validate.unobtrusive.js",
"~/content/plugins/bootstrap/js/bootstrap.js",
};
scriptBundle.Include(scriptArray);
scriptBundle.IncludeDirectory("~/content/js", "*.js");
bundles.Add(scriptBundle);
}
}
Optimizations are set to false when debugging, but are true by default in release mode.
调试时优化设置为 false,但在发布模式下默认为 true。
回答by zenichi
yes, It doesn't work properly with files like *.min.js.
是的,它不适用于 *.min.js 之类的文件。
"if you add a file to your bundle that has a name that ends in .min.js (like I did) and optimizations aren't enabled (e.g. debug is set to true in web.config and BundleTable.EnableOptimizations has not been set to true), this file will be ignored (i.e no script include will be generated for it in your html)."
“如果您将一个文件添加到您的包中,其名称以 .min.js 结尾(就像我所做的那样)并且未启用优化(例如,调试在 web.config 中设置为 true 并且 BundleTable.EnableOptimizations 尚未设置为真),该文件将被忽略(即不会在您的 html 中为它生成脚本包含)。”
Here you can read full original response: http://blog.degree.no/2013/06/javascript-file-missing-resource-bundle-asp-net-web-optimization-framework/
在这里您可以阅读完整的原始回复:http: //blog.degree.no/2013/06/javascript-file-missing-resource-bundle-asp-net-web-optimization-framework/

