前言
新建Asp.net MVC4项目的时候,在Global.asax.cs里面发现多了一句代码
BundleConfig.RegisterBundles(BundleTable.Bundles)
google了以后终于弄清楚了这个的作用,发现这个东西确实非常实用,且功能强大,能够压缩合并js和CSS,但是目前的使用起来不是特别好,如果添加js或者css文件的话,需要修改BundleConfig的代码。
这里我自己简单修改了BundleConfig,对这个进行简单的扩展。
下面贴出代码:
先贴配置文件BundleConfig.xml(文件放在网站目录下路径见代码中变量BundleConfigPath)
复制代码 代码如下:
<?xml version="1.0" encoding="utf-8" ?>
<root>
<Scripts>
<Script Path="~/bundles/jquery">
<File>~/Scripts/jquery-{version}.js</File>
</Script>
<Script Path="~/bundles/jqueryui">
<File>~/Scripts/jquery-ui-{version}.js</File>
</Script>
<Script Path="~/bundles/jqueryval">
<File>~/Scripts/jquery.unobtrusive*</File>
<File>~/Scripts/jquery.validate*</File>
</Script>
<Script Path="~/bundles/modernizr">
<File>~/Scripts/modernizr-*</File>
</Script>
<Script Path="~/bb/aa">
<File>~/Views/Home/addda.js</File>
</Script>
</Scripts>
<Styles>
<Style Path="~/Content/themes/base/css">
<File>~/Content/themes/base/jquery.ui.core.css</File>
<File>~/Content/themes/base/jquery.ui.resizable.css</File>
<File>~/Content/themes/base/jquery.ui.selectable.css</File>
<File>~/Content/themes/base/jquery.ui.accordion.css</File>
<File>~/Content/themes/base/jquery.ui.autocomplete.css</File>
<File>~/Content/themes/base/jquery.ui.button.css</File>
<File>~/Content/themes/base/jquery.ui.dialog.css</File>
<File>~/Content/themes/base/jquery.ui.slider.css</File>
<File>~/Content/themes/base/jquery.ui.tabs.css</File>
<File>~/Content/themes/base/jquery.ui.datepicker.css</File>
<File>~/Content/themes/base/jquery.ui.progressbar.css</File>
<File>~/Content/themes/base/jquery.ui.theme.css</File>
</Style>
<Style Path="~/Content/css">
<File>~/Content/site.css</File>
</Style>
</Styles>
</root>
代码文件:BundleConfig.cs
复制代码 代码如下:
public class BundleConfig
{
public static string BundleConfigPath = "~/Config/BundleConfig.xml";
/// <summary>
/// Register Bundles From XML
/// </summary>
/// <param name="bundles"></param>
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<string> 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<string> files = GetFilesFormNode(node);
if (files.Count > 0)
{
bound.Include(files.ToArray());
bundles.Add(bound);
}
}
}
doc = null;
}
/// <summary>
/// 如果内容为空则不添加
/// </summary>
/// <param name="node"></param>
/// <returns></returns>
private static List<string> GetFilesFormNode(XmlNode node)
{
List<string> files = new List<string>();
foreach (XmlNode nodeFile in node.ChildNodes)
{
if (!string.IsNullOrEmpty(nodeFile.InnerText.Trim()))
files.Add(nodeFile.InnerText.Trim());
}
return files;
}
/// <summary>
/// 检查注册的Node
/// </summary>
/// <param name="node"></param>
/// <returns></returns>
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();
}
}
新建Asp.net MVC4项目的时候,在Global.asax.cs里面发现多了一句代码
BundleConfig.RegisterBundles(BundleTable.Bundles)
google了以后终于弄清楚了这个的作用,发现这个东西确实非常实用,且功能强大,能够压缩合并js和CSS,但是目前的使用起来不是特别好,如果添加js或者css文件的话,需要修改BundleConfig的代码。
这里我自己简单修改了BundleConfig,对这个进行简单的扩展。
下面贴出代码:
先贴配置文件BundleConfig.xml(文件放在网站目录下路径见代码中变量BundleConfigPath)
复制代码 代码如下:
<?xml version="1.0" encoding="utf-8" ?>
<root>
<Scripts>
<Script Path="~/bundles/jquery">
<File>~/Scripts/jquery-{version}.js</File>
</Script>
<Script Path="~/bundles/jqueryui">
<File>~/Scripts/jquery-ui-{version}.js</File>
</Script>
<Script Path="~/bundles/jqueryval">
<File>~/Scripts/jquery.unobtrusive*</File>
<File>~/Scripts/jquery.validate*</File>
</Script>
<Script Path="~/bundles/modernizr">
<File>~/Scripts/modernizr-*</File>
</Script>
<Script Path="~/bb/aa">
<File>~/Views/Home/addda.js</File>
</Script>
</Scripts>
<Styles>
<Style Path="~/Content/themes/base/css">
<File>~/Content/themes/base/jquery.ui.core.css</File>
<File>~/Content/themes/base/jquery.ui.resizable.css</File>
<File>~/Content/themes/base/jquery.ui.selectable.css</File>
<File>~/Content/themes/base/jquery.ui.accordion.css</File>
<File>~/Content/themes/base/jquery.ui.autocomplete.css</File>
<File>~/Content/themes/base/jquery.ui.button.css</File>
<File>~/Content/themes/base/jquery.ui.dialog.css</File>
<File>~/Content/themes/base/jquery.ui.slider.css</File>
<File>~/Content/themes/base/jquery.ui.tabs.css</File>
<File>~/Content/themes/base/jquery.ui.datepicker.css</File>
<File>~/Content/themes/base/jquery.ui.progressbar.css</File>
<File>~/Content/themes/base/jquery.ui.theme.css</File>
</Style>
<Style Path="~/Content/css">
<File>~/Content/site.css</File>
</Style>
</Styles>
</root>
代码文件:BundleConfig.cs
复制代码 代码如下:
public class BundleConfig
{
public static string BundleConfigPath = "~/Config/BundleConfig.xml";
/// <summary>
/// Register Bundles From XML
/// </summary>
/// <param name="bundles"></param>
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<string> 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<string> files = GetFilesFormNode(node);
if (files.Count > 0)
{
bound.Include(files.ToArray());
bundles.Add(bound);
}
}
}
doc = null;
}
/// <summary>
/// 如果内容为空则不添加
/// </summary>
/// <param name="node"></param>
/// <returns></returns>
private static List<string> GetFilesFormNode(XmlNode node)
{
List<string> files = new List<string>();
foreach (XmlNode nodeFile in node.ChildNodes)
{
if (!string.IsNullOrEmpty(nodeFile.InnerText.Trim()))
files.Add(nodeFile.InnerText.Trim());
}
return files;
}
/// <summary>
/// 检查注册的Node
/// </summary>
/// <param name="node"></param>
/// <returns></returns>
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();
}
}
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
暂无评论...
更新日志
2024年11月29日
2024年11月29日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]