首页 > 编程 > .NET > 正文

asp.net Bundle功能扩展

2024-07-10 12:41:12
字体:
来源:转载
供稿:网友
前言
新建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");
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表