The ASP. NET MVC Bundle uses

Source: Internet
Author: User

Basic use

Global.asax

protected void Application_Start ()        {            bundleconfig.registerbundles (bundletable.bundles);        }

Bundleconfig

 Public Static voidregisterbundles (bundlecollection bundles) {bundles. ADD (NewScriptbundle ("~/bundles/jquery"). Include ("~/scripts/jquery-{version}.js")); Bundles. ADD (NewScriptbundle ("~/bundles/bootstrap"). Include ("~/scripts/bootstrap.js",                      "~/scripts/respond.js")); } 

View

@Scripts. Render ("~/bundles/jquery") @Scripts. Render("~/ Bundles/bootstrap")
2 ways to enable bundle (merge compression)
  1. in bundleconfig registerbundles method Add the following code

     public  static  void   Registerbundles (bundlecollection bundles) {bundles. ADD ( new  scriptbundle ( " ~/bundles/jquery   ). Include (  ~/scripts/jquery-{vers    Ion}.js    //  enable merge compression  Bundletable.enableoptimizations = true   
  2. Open web.config The file, set the debug=false compiled

      <system.web>    <compilation debug="false" targetframework=" 4.5 " />  </system.web>
Change Effect

The number of requests is reduced after merging, which in contrast increases the loading speed of the page.

?

Precautions
    • Merge compressed files by default cache for one year. If you repeatedly open the Web page and the configured file is not modified, the server returns an HTTP 304 status code so that the browser loads the files in the cache.

    • Picture path problem, after merging multiple CSS together, sometimes causes the picture to fail to load

      Workaround: Use absolute path for picture

    • BundleConfig Compressed files configured in the configuration are A.js (uncompressed versions), and a.min.js files exist in the same directory. When you modify the A.js file, you want to merge the compressed JS file also contains the modified content, but not as we would like, the compressed JS file or reference to the contents of the A.min.js, previously modified does not appear in the compressed JS file. Similarly, CSS is the same.

References

MVC4 script compression and merging of the new features of ASP.

The ASP. NET MVC Bundle uses

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.