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.
Below is the code:
First paste the configuration file BundleConfig. xml (for the path of the file in the website directory, see the variable BundleConfigPath in the Code)
Copy codeThe Code is as follows: <? 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. csCopy codeThe Code is as follows: 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 ();
}
}