所以我们用插件创建了一个网站,我想知道我是否可以在插件文件夹中搜索任何js文件,并将它们添加到BundleConfig.cs类中的所有包中。
我考虑过将所有想要打包的文件命名为Plugin.pluginName.js,然后搜索所有文件,寻找与"Plugin..js*“匹配的任何文件,但我不确定如何做到这一点。
如何创建插件包?
发布于 2017-06-15 00:41:40
按照命名约定命名所有要绑定的文件的想法是一个很好的遵循的约定。
将脚本包添加到包集合中
public static void RegisterBundles(BundleCollection bundles) {
bundles.Add(new ScriptBundle("~/bundles/plugins").Include(
"~/Scripts/*.Plugin.js")); //{pluginName}.Plugin.js convention
//...other bundles
}如果您想放弃命名约定,也可以在plugins文件夹中搜索任何js文件。
public static void RegisterBundles(BundleCollection bundles) {
bundles.Add(new ScriptBundle("~/bundles/plugins")
.IncludeDirectory("~/Plugins", "*.Plugin.js", true));
//above recursively search subdirectories of directoryVirtualPath.
//...other bundles
}在视图中,使用注册的名称引用包,如
@Scripts.Render("~/bundles/plugins")发布于 2017-06-15 01:34:43
什么是捆绑?
捆绑是ASP.NET 4.5中的一个新特性,它可以轻松地将多个文件组合或捆绑到单个文件中。您可以创建CSS、JavaScript和其他包。更少的文件意味着更少的HTTP请求,这可以提高第一页的加载性能。
如何启用捆绑?
通过在Web.config文件中的编译元素中设置调试属性的值,可以启用或禁用绑定。在下面的XML中,调试设置为true,因此禁用捆绑和缩小。XML
<system.web>
<compilation debug="true" />
</system.web>若要启用捆绑和缩小,请将调试值设置为"false“。
可以使用Web.config类上的EnableOptimizations属性重写BundleTable设置。下面的代码支持捆绑和缩小,并覆盖Web.config文件中的任何设置。
示例
public static void RegisterBundles(BundleCollection bundles) {
bundles.Add(new ScriptBundle("~/bundles/AnyName").Include(
"~/Scripts/Plugins/*.js")); // this will all the files in the plugins folder with .js extension
//you can specify files separately if dont want to use wildcards
BundleTable.EnableOptimizations = true;
}重要注意事项:包含方法中指定的虚拟路径和IncludeDirectory方法中的搜索模式可以接受一个"*“通配符作为前缀或在最后路径段中的后缀。
谢谢
卡蒂克
https://stackoverflow.com/questions/44556755
复制相似问题