擴充asp.net Bundle

來源:互聯網
上載者:User

前言

建立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();
        }
    }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.