asp.net MVC 4 Bundle and Shrink Example Introduction _ Practical Tips

Source: Internet
Author: User
In ASP.net MVC 4, you can bundle multiple CSS and JS files to reduce HTTP requests and compress (shrink) CSS and JS files to increase the speed of your site's loading. Let's take a look at the CSS file in the blog park, and before ASP.net MVC 4, we introduced the CSS method as follows:

Copy Code code as follows:

<link href= "/content/site.css" rel= "stylesheet" type= "Text/css"/>
<link href= "/content/sitehome.css" rel= "stylesheet" type= "Text/css"/>

Use F12 in IE to view the results, as shown in the figure:




In ASP.net MVC 4, use the following notation to introduce CSS files:

<link href= "/content/css" rel= "stylesheet"/>

Rerun, the result is as follows:





It is clear to see that when you introduce CSS using the new method in ASP.net MVC 4, the Web site runs not only with the CSS file but also by compressing the code in the CSS. Double-click the CSS file and you can see in the response body:

JS files are handled in the same way as CSS, the introduction of the following methods:

Copy Code code as follows:

<script src= "/scripts/js" ></script>


asp.net MVC 4 when bundled, the CSS collation is: First load reset.css, NORMALIZE.CSS, other files sorted alphabetically, similar to the JS collation for Jquery.js, jquery-ui.js Other files are also sorted by the first letter. Sometimes we need to load different CSS or JS files on different pages to reduce unnecessary files, let's see how to customize a bundle below.

Add the following code to the Global.asax.cs file Application_Start ():

Copy Code code as follows:

Defines a bundle named "Mycss", and JS corresponds to the new Jsminify ()
var B = new Bundle ("~/mycss", New Cssminify ());
Add all CSS files under the content folder to the bundle
The third argument false indicates that the subfolders under the content folder are not added to the bundle
B.adddirectory ("~/content", "*.css", false);
Add to Bundletable
BUNDLETABLE.BUNDLES.ADD (b);


So in the page you want to use, add the following code:

Copy Code code as follows:

<link href= "/mycss" rel= "Stylesheet"/>


Summary: ASP.net MVC 4 bundled and reduced functionality, so that the reduction of HTTP requests and compression JS, CSS files become very simple, so that we do not need to manually merge JS, CSS files, and do not need to use tools to compress these files, so that JS, CSS file management becomes simple, easy to maintain, Thus freeing us from repetitive work.

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