ASP. NET MVC 4 uses bundle for packaging compression js/css

Source: Internet
Author: User

Packaging (bundling) and compression (minification) refers to the practice of packaging multiple JS files or CSS files into a single file and compressing, so as to reduce the need for the browser to download multiple files in order to complete the page display delay sense, while removing the js/css file in the case of Blank, Annotation and modification of JavaScript internal function, variable name compression method, can effectively reduce file size, improve transmission efficiency, provide users a smoother browsing experience.

You can use Bundletable to bundle multiple CSS files and JS files in ASP. NET MVC 4 to improve the speed of network loading and page resolution. More importantly, through the bundle can solve the Internet Explorer 31 CSS File connection restrictions. Many of the open source JavaScript controls are used when you do an ASP. Virtually adds a reference to CSS and JavaScript files. Merging these CSS files manually will be a big hassle for future versions of the upgrade. Therefore, we have to carefully handle these CSS files in the page reference. Asp. NET Bundle is a new feature of ASP. 4.5, which is under the System.Web.Optimization namespace. He provides some optimizations for the performance aspects of ASP. For example, a page may have many css/js/pictures, through the flexible application Bundletable class, he can help you to optimize the file merge compression code into a most ideal file, and then output to the client, This improves the browser download speed.

First, in the project's App_start folder, there will be a BundleConfig.cs file:

This contains all the content that needs to be bundle, can be set by itself:

One of the bundles. Add is adding bundle entries to the site's bundletable, where there are mainly Scriptbundle and stylebundle, which are used to compress scripts and style sheets, respectively. Initializes an instance of the bundle with a virtual path that does not exist and then, on the basis of the new bundle, include the files in the project. The specific include syntax allows you to review the official profile provided above.

Then the bundle is registered in the project root under the Global.asax file, the file Application_Start is the beginning of the site program, which registers the site of various initialization content, including the bundletable bundle added:

By default, bundles are compressed and packaged with JS and CSS, but there is a property that explicitly describes whether a package compression is required:

Bundletable.enableoptimizations = true;

If you set it to false, it will be the same as the following debug=true.

When used, the Render method of Scriptrender and Stylerender is called at the appropriate location:

End user page to achieve effect packaging compression effect.

There is a place for the main attention, In Web. config, when the Debug property of the compilation compilation is set to True, it means that the project is in debug mode, and the bundle does not compress the files, and the JS and CSS referenced in the page are displayed in HTML as they are, so that it is easy to find problems when debugging (compression After that, it's disgusting ... )。

When the final deployment runs, set debug to False to see that JS and CSS are packaged and compressed.

=============

The key to using bundles is to register bundles with bundletable in ASP.

If you want to use bundles in ASP. NET WebForm, you will need to select the. NET Framework 4.5 When you create a new project, preferably using a template Web site, so you can see the bundle directly.

ASP. NET MVC 4 uses bundle for packaging compression js/css

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.