在ASP.NET MVC项目中,我正在处理与捆绑过程相关的两个应用程序密钥:AppKeys.ApplyMinifyingTransformation
显示.css
和.js
文件应该缩小并组合,AppKeys.ApplyStaticFilesTransformations
显示是否应该应用某些文件内容转换。这些标志的不同组合将用于不同阶段。以下是RegisterBundles
方法的简化版本:
public static void RegisterBundles(BundleCollection bundles)
{
BundleTable.EnableOptimizations = AppKeys.ApplyMinifyingTransformationAndBlockJs ||
AppKeys.ApplyStaticFilesTransformations;
var lessStyles = new Bundle("~/Bundles/Styles/")
.IncludeDirectory("~/Path-to-css", "*.css", true);
var postProcessors = AppKeys.ApplyStaticFilesTransformations
? new[] {new StaticFilesPostProcessor()}
: new IPostProcessor[] {};
var transformer = AppKeys.ApplyMinifyingTransformationAndBlockJs
? new StyleTransformer(new YuiCssMinifier(), postProcessors)
: new StyleTransformer(postProcessors);
transformer.CombineFilesBeforeMinification = AppKeys.ApplyMinifyingTransformationAndBlockJs;
lessStyles.Transforms.Add(transformer);
bundles.Add(lessStyles);
}
不幸的是,此代码无法正常工作。 BundleTable.EnableOptimizations
应为true
,文件转换才能正常工作,但在这种情况下,文件总是合并为一个。
有没有办法明确说明我希望启用转换,但不应合并文件?
答案 0 :(得分:0)
根据 Web.config 中debug
元素的compilation
属性的值,进行捆绑和缩小。
<compilation debug="true" targetFramework="4.0"/>
启用它会使文件按原样呈现,并且debug="false"
文件被捆绑和缩小。
因此,删除显式优化,即删除行
BundleTable.EnableOptimizations = AppKeys.ApplyMinifyingTransformationAndBlockJs ||
AppKeys.ApplyStaticFilesTransformations;
请记住在生产中部署应用程序时,将debug
属性的值更改为false
。
此处有更多信息http://www.asp.net/mvc/overview/performance/bundling-and-minification。