asp.net bundle function Extension _ practical skills

Source: Internet
Author: User
Preface
New ASP.net MVC4 project, in the Global.asax.cs found a more than a code
Bundleconfig.registerbundles (Bundletable.bundles)
Google after finally understand the role of this, found this thing is really very practical, and powerful, can compress merge JS and CSS, but the current use is not particularly good, if you add JS or CSS files, you need to modify the Bundleconfig code.
Here I have simply modified the bundleconfig and made a simple extension of this.
post the code below
First paste the configuration file Bundleconfig.xml (file in the site directory in the path to see the code in the variable Bundleconfigpath)
Copy Code code 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.cs
Copy Code code 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, it is not added
</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 for 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.