首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >忽略MVC Bundles中的文件

忽略MVC Bundles中的文件
EN

Stack Overflow用户
提问于 2015-05-11 15:59:26
回答 5查看 3.3K关注 0票数 5

我们使用功能标志(在Web.Config中设置)来切换某些尚未完成的特性在UI中的可见性。我也希望我的MVC包不包括相关的JS文件,因为它们只是无用的文件,客户端将不得不在功能未启用时下载这些文件。

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

代码语言:javascript
复制
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);
}

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

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2015-05-11 20:29:00

在使用表达式时,我也与忽略列表进行了斗争。我发现的一个简单的工作是实现一个IBundleOrderer,它将排除我不想要的文件,并将一些顺序应用到文件如何被包含上。虽然这并不是它的真正用途,但我发现它填补了空白。

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

代码语言:javascript
复制
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;
    }
}

示例用法:

代码语言:javascript
复制
public static void RegisterBundles(BundleCollection bundles)
{
        var appBundle = new ScriptBundle("~/bundles/app")
            .IncludeDirectory("~/App/", "*.js", true)
        appBundle.Orderer = new ApplicationOrderer();
        bundles.Add(appBundle);
}
票数 4
EN

Stack Overflow用户

发布于 2015-05-11 16:24:40

我认为捆绑代码只发生一次-在网站初始化过程中。如果您正在切换appConfiguration.FeatureXEnabled标志而不重新启动/重新发布网站,则您的更改将被忽略。

一种解决方案是创建两个单独的包,然后使用Razor在HTML中显示正确的包引用。

例如:

代码语言:javascript
复制
@{if (appConfiguration.FeatureXEnabled){
    <script type="text/javascript" src="~/bundles/bundle-large.js"></script>
}else{
    <script type="text/javascript" src="~/bundles/bundle-small.js"></script>
}
票数 2
EN

Stack Overflow用户

发布于 2018-06-17 18:47:29

我把Gent的答案投到了上面,因为它帮助我实现了我想要的。

但是,我对其进行了扩展,并创建了一个通用的IBundleOrderer,它使用lambda来排除任何您想要排除的项目。

注意,排除参数是一个params列表,因此不局限于单个排除模式。这不做任何排序,但可以很容易地添加另一个参数。

代码语言:javascript
复制
public class ExcludeItemsOrderer : IBundleOrderer
{
    private Func<BundleFile, bool>[] _excludes;

    public ExcludeItemsOrderer(params Func<BundleFile, bool>[] excludes)
    {
        _excludes = excludes;
    }

    public IEnumerable<BundleFile> OrderFiles(BundleContext context, IEnumerable<BundleFile> files)
    {
        if(_excludes == null || _excludes.Length == 0)
        {
            return files;
        }

        foreach(var exclude in _excludes)
        {
            var exclusions = files.Where(exclude);
            files = files.Except(exclusions);
        }

        return files;
    }
}

然后我创建了一个扩展方法来简化它的使用:

代码语言:javascript
复制
public static class BundleExtensions
{
    public static Bundle AsIsOrderExcluding(this Bundle bundle, params Func<BundleFile, bool>[] excludes)
    {
        bundle.Orderer = new ExcludeItemsOrderer(excludes);
        return bundle;
    }
}

这样,在创建包时就可以轻松地应用它,如下所示:

代码语言:javascript
复制
bundles.Add(
            new ScriptBundle("~/Bundles/App/js")
                .IncludeDirectory("~/App", "*.js", true)
                .AsIsOrderExcluding(bf => bf.VirtualFile.VirtualPath.IndexOf("/skip_me/") >= 0)
            );
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30172342

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档