我不知道我是否做错了什么,但这可能是 MVC4 中的一个错误。我想知道我该如何解决这个问题?
工作场景
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")
转换为(例如它有效!)
<script src="/js?v=VeCPNK561DZp34yjmWbLrNM35Kf6gaNDl0xsMMC25BQ1"></script>
工作场景不多
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")
转换为(例如它不起作用!)
(nothing, couple of empty break lines)
最佳答案
如果我的理解是正确的,您应该使用“非最小”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);
}
}
优化在调试时设置为 false,但在 Release模式下默认为 true。
关于c# - 如果 EnableOptimization 设置为 true,则 JS 包不会呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13458642/