In the Web page, we often need to refer to a large number of JavaScript and CSS files, plus many JavaScript libraries contain the debug version and the compressed release version (such as jquery), not only the trouble is also easy to cause confusion, so the ASP. MVC4 introduced the bundles feature, which makes it easy to manage JavaScript and CSS files.
It turns out that we reference CSS and JavaScript files and we need a reference like this one:
- <scriptsrc= "~/scripts/jquery-1.8.2.js" ></script>
- <scriptsrc= "~/scripts/jquery-ui-1.8.24.js" ></script>
- <scriptsrc= "~/scripts/jquery.validate.js" ></script>
- <linkhref= "~/content/site.css" rel= "stylesheet"/>
When the number of references required is small, but once each page needs to reference more files, it will cause great inconvenience, when we want to replace a reference file, will waste a lot of time. When publishing, you also need to replace some libraries with release versions, such as the corresponding jquery-1.8.2.min.js of the jquery-1.8.2.js above.
Fortunately, we can now use the bundles feature:
- public class Bundleconfig
- {
- public static void Registerbundles (Bundlecollection bundles)
- {
- Bundles. ADD (New Scriptbundle ("~/bundles/jquery")
- . Include ("~/scripts/jquery-{version}.js"));
- Bundles. ADD (New Scriptbundle ("~/bundles/jqueryui")
- . Include ("~/scripts/jquery-ui-{version}.js"));
- Bundles. ADD (New Scriptbundle ("~/bundles/jqueryval")
- . Include ("~/scripts/jquery.unobtrusive*"
- , "~/scripts/jquery.validate*"));
- Bundles. ADD (New Stylebundle ("~/content/css")
- . Include ("~/content/site.css"));
- }
- }
The Bundleconfig.registerbundles method is then called in the Application_Start method of the Global.asax file:
- protected void Application_Start ()
- {
- Arearegistration.registerallareas ();
- Webapiconfig.register (globalconfiguration.configuration);
- Filterconfig.registerglobalfilters (globalfilters.filters);
- Routeconfig.registerroutes (routetable.routes);
- Bundleconfig.registerbundles (Bundletable.bundles);
- }
In the above we can see that we have different files according to the function of the corresponding bundle (bundle is the meaning of the package), where the string parameter in the constructor is the name of the bundle, the include function is to include the parameter corresponding file into a bundle. It can be found that for the jquery library we used the name ~/scripts/jquery-{version}.js, where {version} section represents the meaning of the version number, and MVC will look for us in the Scripts file for the corresponding " jquery-version number. js "file, and in non-debug mode, MVC uses the" jquery-version number. min.js "file.
We also saw that we used the name ~/scripts/jquery.validate* name, * is a wildcard character, which means that all files prefixed with jquery.validate in the Scripts folder will be included in the same bundle.
Finally, we can use bundles on the view instead of the original reference:
- @Styles. Render ("~/content/css")
- @Scripts. Render ("~/bundles/jquery")
ASP. NET MVC Bundles Learning notes