Using bundles to package compression JS and CSS in ASP.

Source: Internet
Author: User

In the ASP. NET MVC4 (it should also be in WebForm), there is a thing called bundle, it is used to compress JS and CSS (multiple files can be packaged into a file), and can distinguish between debugging and non-debugging, debugging without compression, in the original way to display, To make it easier to find problems.

specific advantages can be self-Baidu or see Official Introduction: Http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification

Here is just a simple record of how to use it.

First, if you are using ASP. NET MVC4 Basic or other content-rich templates, the bundle should already be configured automatically, because it will have a reference to jquery and jquery UI, and these two references will use bundles.

Let's just say the main points.

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.

Use bundles to package compression JS and CSS

in ASP.

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.