我在BundleConfig.config
上定义了几个Bundles虚拟路径,其中每个虚拟路径都关联了几个javascript路径文件。
我想要的是将所有这些虚拟路径加入到一个虚拟路径中。
例如,我有以下代码:
bundles.Add(new ScriptBundle(BundlesVirtualPaths.EditorV2.Main).Include(
"~/Scripts/EditorV2/main.js"
));
bundles.Add(new ScriptBundle(BundlesVirtualPaths.EditorV2.Main2).Include(
"~/Scripts/EditorV2/main2.js"
));
bundles.Add(new ScriptBundle(BundlesVirtualPaths.EditorV2.Main3).Include(
"~/Scripts/EditorV2/main3.js"
));
我想要的是做一些这样的事情:
bundles.Add(new ScriptBundle(BundlesVirtualPaths.EditorV2.AllScripts).Include(
BundlesVirtualPaths.EditorV2.Main,
BundlesVirtualPaths.EditorV2.Main2,
BundlesVirtualPaths.EditorV2.Main3
));
这可以吗?我能做到吗?
答案 0 :(得分:4)
您可以尝试这样的事情:
public static class BundleHelpers
{
public static BundleCollection JoinScriptBundle(this BundleCollection bundleCollection, string virtualPath, params string[] bundlesToJoin)
{
var bundleResolver = new BundleResolver(bundleCollection);
Bundle allScriptBundle = new ScriptBundle(virtualPath);
foreach (var bundle in bundlesToJoin)
{
foreach (var scriptPath in bundleResolver.GetBundleContents(bundle))
{
allScriptBundle = allScriptBundle.Include(scriptPath);
}
}
bundleCollection.Add(allScriptBundle);
return bundleCollection;
}
}
然后你可以加入你的捆绑包:
bundles.JoinScriptBundle("~/bundle/allscript", "~/bundles/jquery", "~/bundles/jqueryval", "~/bundles/modernizr");
答案 1 :(得分:1)
是的,您可以这样做,因为您正在尝试将所有JS
个文件合并到单个bundle
这是正确的方法,但每次都可能不会出现相同的情况。所以你需要根据类型进行捆绑,如下所示
目录:
bundles.Add(new Bundle("~/js/vendor").IncludeDirectory(@"~/Scripts/vendors/JQuery", "*.js")
.IncludeDirectory(@"~/Scripts/vendors/Knockout", "*.js")
.IncludeDirectory(@"~/Scripts/vendors", "*.js"));
对于文件:
bundles.Add(new Bundle("~/js/kendo").Include("~/Scripts/kendo/kendo.core.min.js")
.Include("~/Scripts/kendo/kendo.data.min.js")
.Include("~/Scripts/kendo/kendo.binder.min.js")
.Include("~/Scripts/kendo/kendo.calendar.min.js")
对于网址:
var CdnPath = "http://fonts.googleapis.com/css?family=Open+Sans:300";
bundles.Add(new ScriptBundle("mybarbundle", CdnPath).Include("~/Scripts/jquery-{version}.js"));