如何在不合并文件的情况下启用Bundle转换?

时间:2015-08-27 12:32:39

标签: c# asp.net-mvc bundling-and-minification asp.net-bundling

在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,文件转换才能正常工作,但在这种情况下,文件总是合并为一个。

有没有办法明确说明我希望启用转换,但不应合并文件?

1 个答案:

答案 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