[英]Bundling in asp.net
I'm using bundling in an ASP.net MVC application. 我在ASP.net MVC应用程序中使用捆绑。 I want to build my bundles in a hierarchical fashion. 我想以分层方式构建捆绑包。
For instance, these are the scripts I want on all my webpages: 例如,以下是我想要在所有网页上使用的脚本:
bundles.Add(new ScriptBundle("~/bundles/bootstrap-js").Include(
"~/Scripts/bootstrap.js",
"~/Scripts/respond.js",
"~/Scripts/bootstrap-datetimepicker.min.js",
"~/Scripts/jquery.smartmenus.js",
"~/Scripts/jquery.smartmenus.bootstrap.js"
));
Next I want to have for example knockout on only some pages: 接下来,我想仅在某些页面上进行敲除操作:
bundles.Add(new ScriptBundle("~/bundles/knockout-js").Include(
"~/Scripts/knockout/knockout-3.4.0.js",
"~/Scripts/knockout/knockout-kendo.js",
"~/Scripts/knockout/knockout.mapping-latest.js",
"~/Scripts/knockout/knockout.validation.js",
"~/Scripts/knockout/knockout.validation.de-DE.js",
"~/Scripts/knockout/knockout.validation.fr-BE.js",
"~/Scripts/knockout/knockout.validation.nl-BE.js",
"~/Scripts/knockout/Knockout.bindinghandlers.js",
"~/Scripts/knockout/knockout.validation.mvc.js"));
And finally, I want every page to have it's own custom script. 最后,我希望每个页面都具有自己的自定义脚本。
bundles.Add(new ScriptBundle("~/bundles/scriptName-js").Include(
"~/Views/Cards/scriptName.js"));
Is there a way so I only have to include one @scripts.render statement by combining all previous bundles in to one bundle? 有没有办法让我只需要通过将所有以前的捆绑软件合并为一个捆绑软件来包含一个@ scripts.render语句?
@Scripts.Render("~/bundles/mypage-js")
I tried this but it didn't work: 我试过了,但是没有用:
bundles.Add(new ScriptBundle("~/bundles/mypage-js").Include("~/bundles/scriptName-js","~/bundles/knockout-js","~/bundles/bootstrap-js"));
There is an overload of Include
that accepts an array of paths instead of a params
of paths. Include
重载 ,它接受路径数组而不是路径params
。 You can use this to define blocks of shared scripts as string[]
and then include them: 您可以使用它来将共享脚本块定义为string[]
,然后包括它们:
var everyPage = new [] {
"~/Scripts/bootstrap.js",
"~/Scripts/respond.js",
"~/Scripts/bootstrap-datetimepicker.min.js",
"~/Scripts/jquery.smartmenus.js",
"~/Scripts/jquery.smartmenus.bootstrap.js"
};
var knockout = new [] {
"~/Scripts/knockout/knockout-3.4.0.js",
"~/Scripts/knockout/knockout-kendo.js",
//etc...
};
bundles.Add(new ScriptBundle("~/bundles/scriptName-js")
.Include(everyPage)
.Include(knockout)
.Include("~/Views/Cards/scriptName.js")
);
It's not quite as succinct as referencing one bundle from another but it's better than repeating every block of scripts 它不像从另一个引用一个包那样简洁,但是比重复每个脚本块要好
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.