Introduction of ASP.net MVC4 Bundle bundle Compression technology-practical skills

Source: Internet
Author: User

Very happy, the recent project to use the ASP.net MVC4 + Entity Framework5, found that Mvc4 joined the bundle, Web API and other technologies, really let me excited, used to be a third party, here mainly talk about bundle technology.

Many large sites do not use bundle technology to cause a lot of waste of resources and performance of the sacrifice, do not underestimate the use of you will find his benefits:

Bundle multiple requests into one request, reducing the number of server requests

Without using bundle technology, Debug sees the actual number of requests and paths

Use bundle technology, and have caching capabilities
When debugging is set to release mode and pressing F5 or modifying web.config, you can see the effect of merge and compression

Compress javascript,css and other resource files, reduce network bandwidth and improve performance

Background configuration

MVC4 some changes in the architecture, simplifying the original Global.asax, adding some static configuration files underneath App_start, Note that BundleConfig.cs, as the name implies is bundle configuration, all of its configuration is done here, of course, can also be a separate configuration file.

Copy Code code as follows:
public class Bundleconfig {//For more information on bundling, visit http://go.microsoft.com/fwlink/? linkid=254725 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*")); Use the development version of Modernizr to develop with and learn from. Then, when you are ' RE//ready for production, use the build tool in http://modernizr.com to pick only the tests your need. Bundles. ADD (New Scriptbundle ("~/bundles/modernizr"). Include ("~/scripts/modernizr-*")); Bundles. ADD (New Stylebundle ("~/content/css"). Include ("~/content/site.css")); Bundles. ADD (New Stylebundle ("~/content/themes/base/css"). Include ("~/content/themes/base/jquery.ui.core.css", "~/content/themes/base/jquery.uI.resizable.css "," ~/content/themes/base/jquery.ui.selectable.css "," ~/content/themes/base/ Jquery.ui.accordion.css "," ~/content/themes/base/jquery.ui.autocomplete.css "," ~/content/themes/base/ Jquery.ui.button.css "," ~/content/themes/base/jquery.ui.dialog.css "," ~/content/themes/base/jquery.ui.slider.css "," ~/content/themes/base/jquery.ui.tabs.css, "~/content/themes/base/jquery.ui.datepicker.css", "~/Content/ Themes/base/jquery.ui.progressbar.css "," ~/content/themes/base/jquery.ui.theme.css ")); } }

Here we can according to the modular to configure, we see the following URL corresponds to the above bundles.add (...) added JS, CSS virtualpath

Note that the same resource files that are added by different virtualpath will be loaded repeatedly!

Front Call

For public resource files, we usually put them in the _layout.cshtml (WebForm page) file

Script file Reference: @Scripts. Render (virtualpath[,virtualpath1][,virtualpath2][,...])
CSS file Reference: @Styles. Render (virtualpath[,virtualpath1][,virtualpath2][,...])

Copy Code code as follows:
@Styles. Render ("~/content/css") @Styles. Render ("~/content/themes/base/css")
...
@Scripts. Render ("~/bundles/jquery") @Scripts. Render ("~/bundles/jqueryui") @RenderSection ("Scripts", Required: False

Regular match required, filter not required

Copy Code code as follows:
Bundles. Ignorelist.clear (); Bundles. Ignorelist.ignore ("*.debug.js"); Bundles. Ignorelist.ignore ("*.min.js"); Bundles. Ignorelist.ignore ("*-vsdoc.js"); Bundles. Ignorelist.ignore ("*intellisense.js"); Bundles. ADD (New Scriptbundle ("~/bundles/jquery", JQUERYCDN). Include ("~/scripts/jquery-{version}.js")); Match jquery version bundles. ADD (New Scriptbundle ("~/bundles/jqueryval"). Include ("~/scripts/jquery.unobtrusive*",//matching filename prefix is jquery.unobtrusive "~/scripts/jquery.validate*")); ...

Using CDN

Copy Code code as follows:
Bundles. Usecdn = true; Using CDN String jquerycdn = "Http:deom.jb51.net/jslib/jquery/jquery-1.7.1.min.js"; Bundles. ADD (New Scriptbundle ("~/bundles/jquery", JQUERYCDN). Include ("~/scripts/jquery-{version}.js"));

When the CDN server is hung or inaccessible, the local resource file will be selected here, and debug MVC will let us see his original mask, which is very good for us to debug.
   

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.