栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > asp

asp.net Bundle功能扩展

asp 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

asp.net Bundle功能扩展

前言
新建Asp.net MVC4项目的时候,在Global.asax.cs里面发现多了一句代码
BundleConfig.RegisterBundles(BundleTable.Bundles)
google了以后终于弄清楚了这个的作用,发现这个东西确实非常实用,且功能强大,能够压缩合并js和CSS,但是目前的使用起来不是特别好,如果添加js或者css文件的话,需要修改BundleConfig的代码。
这里我自己简单修改了BundleConfig,对这个进行简单的扩展。
下面贴出代码
先贴配置文件BundleConfig.xml(文件放在网站目录下路径见代码中变量BundleConfigPath)
复制代码 代码如下:










~/Content/themes/base/jquery.ui.core.css
~/Content/themes/base/jquery.ui.resizable.css
~/Content/themes/base/jquery.ui.selectable.css
~/Content/themes/base/jquery.ui.accordion.css
~/Content/themes/base/jquery.ui.autocomplete.css
~/Content/themes/base/jquery.ui.button.css
~/Content/themes/base/jquery.ui.dialog.css
~/Content/themes/base/jquery.ui.slider.css
~/Content/themes/base/jquery.ui.tabs.css
~/Content/themes/base/jquery.ui.datepicker.css
~/Content/themes/base/jquery.ui.progressbar.css
~/Content/themes/base/jquery.ui.theme.css


~/Content/site.css




代码文件:BundleConfig.cs
复制代码 代码如下:
public class BundleConfig
{
public static string BundleConfigPath = "~/Config/BundleConfig.xml";
///
/// Register Bundles From XML
///

///
public static void RegisterBundles(BundleCollection bundles)
{
Xmldocument doc = new Xmldocument();
doc.Load(HttpContext.Current.Server.MapPath(BundleConfigPath));
XmlNode root = doc.documentElement;
// Regester script
XmlNodeList scriptList = root.SelectNodes("scripts/script");
if (scriptList != null && scriptList.Count > 0)
{
foreach (XmlNode node in scriptList)
{
string path = CheckNodeRegedit(node);
if (string.IsNullOrEmpty(path)) continue;
var bound = new scriptBundle(path);
List files = GetFilesFormNode(node);
if (files.Count > 0)
{
bound.Include(files.ToArray());
bundles.Add(bound);
}
}
}
// Regester Style
XmlNodeList StyleList = root.SelectNodes("Styles/Style");
if (StyleList != null && StyleList.Count > 0)
{
foreach (XmlNode node in StyleList)
{
string path = CheckNodeRegedit(node);
if (string.IsNullOrEmpty(path)) continue;
var bound = new StyleBundle(path);
List files = GetFilesFormNode(node);
if (files.Count > 0)
{
bound.Include(files.ToArray());
bundles.Add(bound);
}
}
}
doc = null;
}
///
/// 如果内容为空则不添加
///

///
///
private static List GetFilesFormNode(XmlNode node)
{
List files = new List();
foreach (XmlNode nodeFile in node.ChildNodes)
{
if (!string.IsNullOrEmpty(nodeFile.InnerText.Trim()))
files.Add(nodeFile.InnerText.Trim());
}
return files;
}
///
/// 检查注册的Node
///

///
///
private static string CheckNodeRegedit(XmlNode node)
{
XmlAttribute pathAtt = node.Attributes["Path"];
string path = string.Empty;
if (pathAtt == null || string.IsNullOrEmpty(pathAtt.Value.Trim()) || node.ChildNodes.Count == 0)
return string.Empty;
else
return pathAtt.Value.Trim();
}
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/58258.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号