当前位置:Gxlcms > asp.net > asp.net Bundle功能扩展

asp.net Bundle功能扩展

时间:2021-07-01 10:21:17 帮助过:37人阅读

前言
新建Asp.net MVC4项目的时候,在Global.asax.cs里面发现多了一句代码
BundleConfig.RegisterBundles(BundleTable.Bundles)
google了以后终于弄清楚了这个的作用,发现这个东西确实非常实用,且功能强大,能够压缩合并js和CSS,但是目前的使用起来不是特别好,如果添加js或者css文件的话,需要修改BundleConfig的代码。
这里我自己简单修改了BundleConfig,对这个进行简单的扩展。
下面贴出代码
先贴配置文件BundleConfig.xml(文件放在网站目录下路径见代码中变量BundleConfigPath)
代码如下:
  1. <br><?xml version="1.0" encoding="utf-8" ?> <br><root> <br><Scripts> <br><Script Path="~/bundles/jquery"> <br><File>~/Scripts/jquery-{version}.js</File> <br></Script> <br><Script Path="~/bundles/jqueryui"> <br><File>~/Scripts/jquery-ui-{version}.js</File> <br></Script> <br><Script Path="~/bundles/jqueryval"> <br><File>~/Scripts/jquery.unobtrusive*</File> <br><File>~/Scripts/jquery.validate*</File> <br></Script> <br><Script Path="~/bundles/modernizr"> <br><File>~/Scripts/modernizr-*</File> <br></Script> <br><Script Path="~/bb/aa"> <br><File>~/Views/Home/addda.js</File> <br></Script> <br></Scripts> <br><Styles> <br><Style Path="~/Content/themes/base/css"> <br><File>~/Content/themes/base/jquery.ui.core.css</File> <br><File>~/Content/themes/base/jquery.ui.resizable.css</File> <br><File>~/Content/themes/base/jquery.ui.selectable.css</File> <br><File>~/Content/themes/base/jquery.ui.accordion.css</File> <br><File>~/Content/themes/base/jquery.ui.autocomplete.css</File> <br><File>~/Content/themes/base/jquery.ui.button.css</File> <br><File>~/Content/themes/base/jquery.ui.dialog.css</File> <br><File>~/Content/themes/base/jquery.ui.slider.css</File> <br><File>~/Content/themes/base/jquery.ui.tabs.css</File> <br><File>~/Content/themes/base/jquery.ui.datepicker.css</File> <br><File>~/Content/themes/base/jquery.ui.progressbar.css</File> <br><File>~/Content/themes/base/jquery.ui.theme.css</File> <br></Style> <br><Style Path="~/Content/css"> <br><File>~/Content/site.css</File> <br></Style> <br></Styles> <br></root> <br> <br>代码文件:BundleConfig.cs <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br>public class BundleConfig <br>{ <br>public static string BundleConfigPath = "~/Config/BundleConfig.xml"; <br>/// <summary> <br>/// Register Bundles From XML <br>/// </summary> <br>/// <param name="bundles"></param> <br>public static void RegisterBundles(BundleCollection bundles) <br>{ <br>XmlDocument doc = new XmlDocument(); <br>doc.Load(HttpContext.Current.Server.MapPath(BundleConfigPath)); <br>XmlNode root = doc.DocumentElement; <br>// Regester Script <br>XmlNodeList ScriptList = root.SelectNodes("Scripts/Script"); <br>if (ScriptList != null && ScriptList.Count > 0) <br>{ <br>foreach (XmlNode node in ScriptList) <br>{ <br>string path = CheckNodeRegedit(node); <br>if (string.IsNullOrEmpty(path)) continue; <br>var bound = new ScriptBundle(path); <br>List<string> files = GetFilesFormNode(node); <br>if (files.Count > 0) <br>{ <br>bound.Include(files.ToArray()); <br>bundles.Add(bound); <br>} <br>} <br>} <br>// Regester Style <br>XmlNodeList StyleList = root.SelectNodes("Styles/Style"); <br>if (StyleList != null && StyleList.Count > 0) <br>{ <br>foreach (XmlNode node in StyleList) <br>{ <br>string path = CheckNodeRegedit(node); <br>if (string.IsNullOrEmpty(path)) continue; <br>var bound = new StyleBundle(path); <br>List<string> files = GetFilesFormNode(node); <br>if (files.Count > 0) <br>{ <br>bound.Include(files.ToArray()); <br>bundles.Add(bound); <br>} <br>} <br>} <br>doc = null; <br>} <br>/// <summary> <br>/// 如果内容为空则不添加 <br>/// </summary> <br>/// <param name="node"></param> <br>/// <returns></returns> <br>private static List<string> GetFilesFormNode(XmlNode node) <br>{ <br>List<string> files = new List<string>(); <br>foreach (XmlNode nodeFile in node.ChildNodes) <br>{ <br>if (!string.IsNullOrEmpty(nodeFile.InnerText.Trim())) <br>files.Add(nodeFile.InnerText.Trim()); <br>} <br>return files; <br>} <br>/// <summary> <br>/// 检查注册的Node <br>/// </summary> <br>/// <param name="node"></param> <br>/// <returns></returns> <br>private static string CheckNodeRegedit(XmlNode node) <br>{ <br>XmlAttribute pathAtt = node.Attributes["Path"]; <br>string path = string.Empty; <br>if (pathAtt == null || string.IsNullOrEmpty(pathAtt.Value.Trim()) || node.ChildNodes.Count == 0) <br>return string.Empty; <br>else <br>return pathAtt.Value.Trim(); <br>} <br>} <br></li><li> </li><li> </li></ol></pre>

人气教程排行