asp.net MVC use bundle to package the compression JS and CSS Method _ practical skills

Source: Internet
Author: User

In the ASP.net MVC4 (should also have in the 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 debugging, debugging without compression, the original way to display, To make it easier to find problems.

Specific advantages can be their own Baidu or refer to the official introduction:http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification

Here is only a simple record of how to use.

First, if you are using a asp.net MVC4 basic or other richer template, bundle should have been configured automatically because of the jquery and jquery UI references that will use bundle.

Let's just say the main point.

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

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

One of the bundles. Add is a bundle entry in the bundletable to the Web site, where Scriptbundle and stylebundle are used to compress scripts and style sheets, respectively. Initializes an instance of the bundle with a virtual path, which is not real, and then goes in with the file included in the project on the new bundle basis. The specific include syntax can refer to the official profile provided above.

Then the bundle registration is in the project root under the Global.asax file, the file Application_Start is the beginning of the Web site program, which registered a variety of Web site initialization content, including the bundletable bundle add:

By default, bundle is packaged for JS and CSS, but there is a property that explicitly describes whether you need to package the compression:

Copy Code code as follows:
Bundletable.enableoptimizations = true;

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

When used, call the Scriptrender and Stylerender render method at the appropriate location:

End user page to achieve effect packaging compression effect.

There is one place where the main attention In Web.config, when the Debug property of the compilation compilation is set to True, the project is in debug mode, and bundle does not package the file for compression, the JS and CSS referenced in the page are scattered as shown in HTML, so it is convenient to find the problem when debugging (compress It's complicated later.)

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

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

The key to using bundle is to register bundle with the bundletable in asp.net.

If you want to use bundle in asp.net webform, you need to select the. NET Framework 4.5 When you create a new project, and it is best to use the template Web site to create new ones so you can see bundle directly.

The above is the entire content of this article, I hope to help you learn.

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.