0

在我正在处理的 ASP.NET MVC 项目中,有两个与捆绑过程相关的应用程序键:AppKeys.ApplyMinifyingTransformation显示是否应缩小和合并文件,.css显示是否应应用某些文件内容转换。这些标志的不同组合将用于不同阶段。这是该方法的简化版本:.jsAppKeys.ApplyStaticFilesTransformationsRegisterBundles

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

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

4

1 回答 1

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

于 2015-08-27T13:32:52.953 回答