【问题标题】:How to enable Bundle transformation without combining files?如何在不合并文件的情况下启用捆绑转换?
【发布时间】:2015-08-27 12:32:39
【问题描述】:

在我正在处理的 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 以使文件转换工作,但在这种情况下,文件总是合并为一个。

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

【问题讨论】:

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


    【解决方案1】:

    根据Web.configcompilation元素的debug属性值进行捆绑和缩小。

    <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

    【讨论】:

    • 对不起,但这一点帮助也没有。首先,我相信该设置成为appSettings 部分中的众多键之一更加透明,这仅仅是因为我们的转换文件是如何编写的。其次,无论该设置到底是在哪里设置的,它都不会改变系统的行为方式:使用debug="true" 我既没有得到转换也没有得到缩小,否则我得到了它们。这不是在应用所需转换时避免将文件合并为一个的方法。
    猜你喜欢
    • 1970-01-01
    • 2020-11-16
    • 1970-01-01
    • 2016-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多