Using bundles to package the compression JS and CSS (go) in asp.

Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="font-family: 微软雅黑; font-size: medium;">Transferred from:</span></p></p><p><p></p></p><p><p><span style="font-family: 微软雅黑; font-size: medium;">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.</span></p></p><p><p><span style="font-family: 微软雅黑; font-size: medium;">specific advantages can be Self-baidu or see</span> <span style="font-family: 微软雅黑; font-size: medium;">official introduction:</span></p></p><p><p><span style="font-family: 微软雅黑; font-size: medium;">Here is just a simple record of how to use it. (author: The route manager is the management of the controller resource mapping, bundle is the management of JS and CSS Files)</span></p></p><p><p><span style="font-family: 微软雅黑; font-size: medium;">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.</span></p></p><p><p><span style="font-family: 微软雅黑; font-size: medium;">Let's just say the main points.</span></p></p><p><p><span style="font-family: 微软雅黑; font-size: medium;">first, in the Project's App_start folder, there will be a BundleConfig.cs file:</span></p></p><p><p></p></p><p><p><span style="font-family: 微软雅黑; font-size: medium;">This contains all the content that needs to be bundle, can be set by itself:</span></p></p><p><p></p></p><p><p><span style="font-family: 微软雅黑; font-size: medium;">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. </span> <span style="font-family: 微软雅黑; font-size: medium;">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. </span></p></p><p><p><span style="font-family: 微软雅黑; font-size: medium;">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:</span></p></p><p><p></p></p><p><p><span style="font-family: 微软雅黑; font-size: medium;">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:</span></p></p><pre><pre>Bundletable.enableoptimizations = true;</pre></pre><p><p><span style="font-family: 微软雅黑; font-size: medium;">If you set it to false, it will be the same as the following Debug=true.</span></p></p><p><p><span style="font-family: 微软雅黑; font-size: medium;">When used, the Render method of Scriptrender and Stylerender is called at the appropriate location:</span></p></p><p><p></p></p><p><p><span style="font-family: 微软雅黑; font-size: medium;">End user page to achieve effect packaging compression Effect.</span></p></p><p><p><span style="font-family: 微软雅黑; font-size: medium;">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 ... )。</span></p></p><p><p></p></p><p><p><span style="font-family: 微软雅黑; font-size: medium;">When the final deployment runs, set debug to False to see that JS and CSS are packaged and Compressed.</span></p></p><p><p><span style="font-family: 微软雅黑; font-size: medium;">=============</span></p></p><p><p><span style="font-family: 微软雅黑; font-size: medium;">The key to using bundles is to register bundles with bundletable in asp.</span></p></p><p><p><span style="font-family: 微软雅黑; font-size: medium;">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.</span></p></p><p><p><span style="font-family: 微软雅黑; font-size: medium;"></span></p></p><p><p>Using bundles to package the compression JS and CSS (go) in asp.</p></p></span>

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: 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.