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); 

在我的 Scripts 文件夹中具有以下结构。
请注意,jQuery 与其他 jQuery 处于同一级别。

文件夹 JS 结构

_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

4

1 回答 1

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");

希望它可以帮助你。

于 2013-09-11T10:44:14.260 回答