忽略MVC包中的文件

CBa*_*arr 5 c# asp.net-mvc bundling-and-minification

我们使用功能标记(在Web.Config中设置)来切换UI中某些尚未完成的功能的可见性.我还希望我的MVC包不包含相关的JS文件,因为它们只是在没有启用该功能时客户端必须下载的无用文件.

到目前为止,我已经找到了,IgnoreList.Ignore但我似乎无法让它工作.这基本上就是我在做的事情:

public static void RegisterBundles(BundleCollection bundles)
{
    if (!appConfiguration.FeatureXEnabled)
    {
        //Skip these files if the Feature X is not enabled!
        //When this flag is removed, these lines can be deleted and the files will be included like normal
        bundles.IgnoreList.Ignore("~/App/Organization/SomeCtrl.js", OptimizationMode.Always);
        bundles.IgnoreList.Ignore("~/App/Filters/SomeFilter.js", OptimizationMode.Always);
    }

    var globalBundle = new ScriptBundle("~/bundles/app-global").Include(
        "~/App/RootCtrl.js",
        "~/App/Directives/*.js",
        "~/App/Services/*.js",
        "~/App/Application.js"
    );
    bundles.Add(globalBundle);

    var appOrgBundle = new ScriptBundle("~/bundles/app-org");
    appOrgBundle.Include(
        "~/App/Filters/*.js",
        "~/App/Organization/*.js"
    );

    bundles.Add(appOrgBundle);
}
Run Code Online (Sandbox Code Playgroud)

使用上面的代码,仍然包含忽略列表中的文件!我在这做错了什么?

Gen*_*ent 4

我在使用表达式时也曾与忽略列表作过斗争。我发现的一个简单的解决方法是实现一个IBundleOrderer排除我不需要的文件的方法,并对文件的包含方式应用一些排序。虽然这并不是它的真正用途,但我发现它填补了空白。

可以IBundleOrderer访问完整的文件列表(表达式扩展到它匹配的文件)。

public class ApplicationOrderer : IBundleOrderer {
    public IEnumerable<BundleFile> OrderFiles(BundleContext context, IEnumerable<BundleFile> files)
    {
        if (!AppSettings.FeatureFlag_ServiceIntegrationsEnabled)
        {
            //Skip these files if the Service Integrations Feature is not enabled!
            //When this flag is removed, these lines can be deleted and the files will be included like normal
            var serviceIntegrationPathsToIgnore = new[]
            {
                "/App/ServiceIntegrations/IntegrationSettingsModel.js",
                "/App/ServiceIntegrations/IntegrationSettingsService.js",
                "/App/ServiceIntegrations/ServiceIntegrationsCtrl.js"
            };
            files = files.Where(x => !serviceIntegrationPathsToIgnore.Contains(x.VirtualFile.VirtualPath));
        }

        return files;
    }
}
Run Code Online (Sandbox Code Playgroud)

用法示例:

public static void RegisterBundles(BundleCollection bundles)
{
        var appBundle = new ScriptBundle("~/bundles/app")
            .IncludeDirectory("~/App/", "*.js", true)
        appBundle.Orderer = new ApplicationOrderer();
        bundles.Add(appBundle);
}
Run Code Online (Sandbox Code Playgroud)