Asp.net mvc css/Javascript Bundle configuration file, mvcbundle
In Asp.net mvc, Web Optimization can be used to merge and compress JS and CSS files, but the Code must be changed every time you modify the code ~ /App_Start/BundleConfig. cs, and the idea of moving it to the configuration file
The idea is simple. Make an XML configuration file to save it.
First, let's take a look at two types of Bundle: StyleBundle and ScriptBundle. Therefore, two types of Bundle must be supported in the configuration file, add the file path.
1) determine the XML format, as shown below:
<?xml version="1.0" encoding="utf-8" ?><bundles xmlns="http://www.mobwiz.net/Schema/BundleConfig"> <cssbundles> <bundle> <name>~/content/backstage</name> <files> <file>~/Content/bootstrap.min.css</file> <file>~/Content/bootstrap-theme.min.css</file> <file>~/Content/bootstrap-datetimepicker.min.css</file> <file>~/Content/treetable/jquery.treetable.css</file> <file>~/Content/treetable/jquery.treetable.theme.default.css</file> <file>~/Content/admin.css</file> <file>~/Content/uploadify.css</file> </files> </bundle> <bundle> <name>~/content/portal</name> <files> <file>~/Content/bootstrap.min.css</file> <file>~/Content/bootstrap-theme.min.css</file> <file>~/Content/portal.css</file> </files> </bundle> </cssbundles> <scriptbundles> <bundle> <name>~/script/backjs</name> <files> <file>~/Scripts/jquery-1.11.2.min.js</file> <file>~/Scripts/bootstrap.min.js</file> <file>~/Scripts/jquery.cookie.js</file> <file>~/Scripts/jquery.validate.min.js</file> <file>~/Scripts/jquery.validate.unobtrusive.min.js</file> <file>~/Scripts/jquery.validate.unobtrusive.bootstrap.min.js</file> <file>~/Scripts/jquery.treetable.js</file> <file>~/Scripts/jquery.uploadify.min.js</file> <file>~/Scripts/json2.js</file> <file>~/Scripts/jquery.autosize-min.js</file> <file>~/Scripts/jquery.ba-bbq.js</file> <file>~/Scripts/mwext.js</file> <file>~/Scripts/jquery.uploadify.min.js</file> <file>~/Scripts/moment.min.js</file> <file>~/Scripts/moment-with-locales.min.js</file> <file>~/Scripts/bootstrap-datetimepicker.min.js</file> <file>~/Scripts/bootstrap-treeview.min.js</file> </files> </bundle> <bundle> <name>~/script/keditor</name> <files> <file>~/kindeditor/kindeditor-all-min.js</file> <file>~/kindeditor/lang/zh_CN.js</file> </files> </bundle> <bundle> <name>~/script/fixie</name> <files> <file>~/Script/html5shiv.min.js</file> <file>~/Script/respond.min.js</file> </files> </bundle> </scriptbundles></bundles>
2) read the code
public class BundleConfig { // For more information on bundling, visit http://go.microsoft.com/fwlink/?LinkId=301862 public static void RegisterBundles(BundleCollection bundles) { var path = HttpContext.Current.Server.MapPath("~/App_Data/bundles.config"); XElement rootEL = XElement.Load(path); XNamespace ns = rootEL.Attribute("xmlns").Value; Debug.Assert(rootEL != null); var cssBundles = rootEL.Element(ns + "cssbundles"); if (cssBundles != null && cssBundles.HasElements) { var list = cssBundles.Elements(ns + "bundle"); if (list != null) { foreach (var xElement in list) { var name = xElement.Element(ns + "name").Value; var files = xElement.Element(ns + "files").Elements(ns + "file"); var fileList = new List<string>(); foreach (var element in files) { fileList.Add(element.Value); } bundles.Add(new StyleBundle(name).Include(fileList.ToArray())); } } } var scriptBundles = rootEL.Element(ns + "scriptbundles"); if (scriptBundles != null && scriptBundles.HasElements) { var list = scriptBundles.Elements(ns + "bundle"); if (list != null) { foreach (var xElement in list) { var name = xElement.Element(ns + "name").Value; var files = xElement.Element(ns + "files").Elements(ns + "file"); var fileList = new List<string>(); foreach (var element in files) { fileList.Add(element.Value); } bundles.Add(new ScriptBundle(name).Include(fileList.ToArray())); } } } } }
This code is very rough. You will take a look at it. The principle is to read the configuration in xml and add it to BundleConfig.
3) Write a Schema
Because it is a configuration file, make a Schema, and write it in VS, there will also be a syntax prompt to avoid errors. XSD uses XML Spy to upload files, if no attachment is found, the code is pasted, copied, saved to an xsd, and placed in the Schema directory of.
<? Xml version = "1.0" encoding = "UTF-8"?> <Xs: schema xmlns: xs =" http://www.w3.org/2001/XMLSchema "TargetNamespace =" http://www.mobwiz.net/Schema/BundleConfig "Xmlns =" http://www.mobwiz.net/Schema/BundleConfig "ElementFormDefault =" qualified "attributeFormDefault =" unqualified "> <xs: element name =" bundles "> <xs: annotation> <xs: documentation> Root element for bundles </xs: documentation> </xs: annotation> <xs: complexType> <xs: all> <xs: element name = "cssbundles"> <xs: complexType> <xs: sequence> <xs: element name = "bundle" type = "bundleType" maxOccurs = "unbounded"/> </xs: sequence> </xs: complexType> </xs: element> <xs: element name = "scriptbundles"> <xs: complexType> <xs: sequence> <xs: element name = "bundle" type = "bundleType" maxOccurs = "unbounded"/> </xs: sequence> </xs: complexType> </xs: element> </xs: all> </xs: complexType> </xs: element> <xs: complexType name = "bundleType"> <xs: annotation> <xs: documentation> Bundle Type </xs: documentation> </xs: annotation> <xs: sequence> <xs: element name = "name" type = "xs: string"/> <xs: element name = "files"> <xs: complexType> <xs: sequence> <xs: element name = "file" type = "xs: string "maxOccurs =" unbounded "/> </xs: sequence> </xs: complexType> </xs: element> </xs: sequence> </xs: complexType> </xs: schema>View Code
4) update changes
This is not implemented yet. The idea is to use FileSystemWatcher to monitor the configuration file. If the file is changed, load the configuration again. Of course, you can simply and rudely restart the website to reload it.
5) Reference
This does not change. You can use the following code to call it on the page.
@Styles.Render("~/content/backstage")@Scripts.Render("~/script/backjs")
2015.06.17