2013-05-07 52 views
2

设置我束如下单个文件:忽略在束

bundles.UseCdn = true; 

var scriptJquery = new ScriptBundle(Bundles.Scripts.Jquery, "//code.jquery.com/jquery-1.9.1.js") 
    { 
     CdnFallbackExpression = "window.jQuery", 
    } 
    .Include("~/scripts/jquery-{version}.js"); // {version} not work! 

bundles.Add(scriptJquery); 

var scriptBundle = new ScriptBundle(Bundles.Scripts.Common) 
    { 
     Orderer = new FileBundleOrderer(server.MapPath("~/Scripts/bundle.txt")) 
    } 
    .IncludeDirectory("~/Scripts", "*.js", false); 
bundles.Add(scriptBundle); 

以我的脚本文件夹具有以下结构。
请注意,jQuery与其他人在同一水平。

Folder JS Structure

_Layout.cshtml

@Scripts.Render(CreditoImobiliarioBB.Web.App_Start.Bundles.Scripts.Jquery) 
@Scripts.Render(CreditoImobiliarioBB.Web.App_Start.Bundles.Scripts.Common) 

当运行产生以下脚本中的应用。 请注意,jQuery出现两次!

<script src="/scripts/jquery-1.9.1.js"></script> <!-- <------ --> 

<script src="/Scripts/moment.js"></script> 
<script src="/Scripts/moment.pt-br.js"></script> 
<script src="/Scripts/handlebars.runtime.js"></script> 
<script src="/Scripts/knockout-2.2.1.debug.js"></script> 
<script src="/Scripts/knockout.mapping-latest.js"></script> 
<script src="/Scripts/underscore.js"></script> 
<script src="/Scripts/bootstrap.js"></script> 
<script src="/Scripts/bootstrap-datepicker.js"></script> 
<script src="/Scripts/bootstrap-datepicker.pt-BR.js"></script> 
<script src="/Scripts/bootstrap-multiselect.js"></script> 
<script src="/Scripts/bootstrap-typeahead.js"></script> 
<script src="/Scripts/accounting.js"></script> 
<script src="/Scripts/jquery.pnotify.js"></script> 
<script src="/Scripts/fileuploader.js"></script> 
<script src="/Scripts/jquery.inputmask.js"></script> 
<script src="/Scripts/jquery.inputmask.extensions.js"></script> 
<script src="/Scripts/jquery.inputmask.numeric.extensions.js"></script> 
<script src="/Scripts/jquery.inputmask.date.extensions.js"></script> 
<script src="/Scripts/jquery.maskMoney.js"></script> 
<script src="/Scripts/jquery.bootpag.js"></script> 
<script src="/Scripts/Email.js"></script> 
<script src="/Scripts/Endereco.js"></script> 
<script src="/Scripts/Telefone.js"></script> 
<script src="/Scripts/prototypes.js"></script> 
<script src="/Scripts/antiForgetyTokenHelper.js"></script> 
<script src="/Scripts/ajaxPost.js"></script> 
<script src="/Scripts/ajaxUploader.js"></script> 
<script src="/Scripts/jquery-1.9.1.js"></script> <!-- <------ --> 
<script src="/Scripts/jquery.cycle.all.js"></script> 
<script src="/Scripts/scripts.js"></script> 

如果我试图忽略jQuery的文件,它不会出现在任何方式!

bundles.IgnoreList.Ignore("jquery-{version}.js", OptimizationMode.Always); 

如何只在一个包中忽略jQuery?在Bundles.Scripts.Common

+1

您可以在这里尝试乔恩·马尔科姆的回答是: http://stackoverflow.com/questions/13990622/asp-net-mvc-exclude-css-file-from-bundle – 2013-05-07 14:15:43

+0

@SteveTemple它的帮助。我在想'Exclude =“jquery。{version} *。js”'。 – ridermansb 2013-05-07 14:37:42

+0

你可以修改上面url中列出的扩展方法 – 2013-05-08 17:17:00

回答

3

我已经扩展@Jon Malcolm答案来解决我的问题。

public static class BundleExtentions 
{ 
    public static Bundle IncludeDirectoryWithExclusion(this ScriptBundle bundle, string directoryVirtualPath, string searchPattern, bool includeSubDirectories, params string[] toExclude) 
    { 
     var folderPath = HttpContext.Current.Server.MapPath(directoryVirtualPath); 
     SearchOption searchOption = includeSubDirectories 
             ? SearchOption.AllDirectories 
             : SearchOption.TopDirectoryOnly; 

     foreach (var file in from file in Directory.GetFiles(folderPath, searchPattern, searchOption) let fileName = Path.GetFileName(file) where String.IsNullOrEmpty(Array.Find(toExclude, s => fileName != null && s.ToLower() == fileName.ToLower())) select file) 
     { 
      bundle.Include(directoryVirtualPath + file.Replace(folderPath, string.Empty).Replace("\\", "/")); 
     } 

     return bundle; 
    } 
} 

使用

mybundle.IncludeDirectoryWithExclusion("~/Directory", "*.js", true, "excluejsfile.js"); 

希望它可以帮助你。

+1

我已经改进了这个代码,甚至进一步添加排除模式和正确的.min处理在这里:http://stackoverflow.com/a/36080620/1671558 – 2016-03-18 09:29:58