Extended asp.net Bundle

Source: Internet
Author: User

Preface

When creating the Asp.net MVC4 project, I found an additional code in Global. asax. cs.

BundleConfig. RegisterBundles (BundleTable. Bundles)

After google finally figured out its role and found that it is indeed very practical and powerful, and can compress and merge js and CSS, but it is not very good at present, to add a js or css file, you need to modify the BundleConfig code.

Here, I have modified BundleConfig to perform simple extension.

The following code is provided:

First paste the configuration file BundleConfig. xml (for the path of the file in the website directory, see the variable BundleConfigPath in the Code)

<? 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>

Code File: 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>
/// If the content is empty, do not add
/// </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>
/// Check the registered 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 ();
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.