Tuesday, September 4, 2012

Using ASP.NET Bundling and Minification with Web Forms

Bundling and Minification is a new feature of ASP.NET 4.
Learn more from this tutorial http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification.

It has been promoted as part of ASP.NET MVC 4, but actually does not depend on MVC stack and may by used within any ASP.NET application including ASP.NET Web Forms.

When you start a new ASP.NET MVC 4 application using Visual Studio template, it sets up all what you need to start using this feature, but if you have an existing ASP.NET application (even not MVC), you have to complete following steps to make it works with your app.


Adding reference to System.Web.Optimization library

System.Web.Optimization is not a part of .NET 4 framework. But it is provisioned by NuGet package manager.

Open NuGet and search for Microsoft.AspNet.Web.Optimization package.



As you may see, it depends on two other packages, but you don't have to worry, NuGet will manage all dependencies and will install them as well.


Now you may see a few new references added to your project:  Antlr3.Runtime, Microsoft.Web.Infrastructure, System.Web.Optimization and WebGrease.


All new referenced are set to be copied to /bin folder (Copy Local = true in property window), so they are deployed to server with your application. No need to mess with GAC :).

Register bundle


All the rest is according to tutorial. Register bundles with Application_Start:

Include bundle in the page

In my scenario I use Web Forms and have an *.aspx with a master page. To include the bundle in the page, it is simply enough to add one line of code within head tag:


Runing application

Now you can run the application. Depending on debug settings in Web.config you will get either all scripts (included in the bundle) included separately in the page (debug="true") for debugging purpose, or single all-in-one minified script include

CSS bundling

In the same manner you can use CSS bundling

Register StyleBundle:


Include in the page:


62 comments:

  1. hi, im really struggling with this and wondering if you can help. im doing this on a vs2010 website in WEBFORMS and iv got the package from nuget. here is my code in my global.asax:

    void Application_Start(object sender, EventArgs e)
    {
    // Code that runs on application startup

    System.Web.Optimization.BundleTable.Bundles.Add(new System.Web.Optimization.ScriptBundle("~/bundles/js").Include("~/scripts/jquery.bxSlider.js"));

    }

    and my script:

    <%: System.Web.Optimization.Scripts.Render("~/bundles/js") %>

    But the bundle script tag doesn't even render... any ideas?

    ReplyDelete
    Replies
    1. Do you have a repro? You may send me a sample project for debugging to igor.israel@gmail.com

      Delete
    2. thanks igor, problem is its a large corporate site! however does it help in saying its a website rather than a web project, can that make a difference?

      Delete
    3. No difference between web site and web project.

      Delete
    4. I am glad that I saw this post. It is informative blog for us and we need this type of blog thanks for share this blog, Keep posting such instructional blogs and I am looking forward for your future posts. Python Projects for Students Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account. Project Center in Chennai

      Delete
  2. hanks for sharing this. We really want to find a good website design company. Our business could really use one. What would you suggest?
    Asp.net development company Delhi

    ReplyDelete
  3. Ecorptrainings.com provides ASP.NET in hyderabad with best faculties on real time projects. We give the best online trainingamong the ASP.NET in Hyderabad. Classroom Training in Hyderabad India

    ReplyDelete
  4. Great post full of useful tips! My site is fairly new and I am also having a hard time getting my readers to leave comments. Analytics shows they are coming to the site but I have a feeling “nobody wants to be first”.
    asp net development in ukraine

    ReplyDelete

  5. Thanks for Sharing This Article.It is very so much valuable content. I hope these Commenting lists will help to my website
    top workday studio online training

    ReplyDelete
  6. I just loved your article on the beginners guide to starting a blog.If somebody take this blog article seriously in their life, he/she can earn his living by doing blogging.thank you for thizs article. devops online training

    ReplyDelete
  7. Very informative and creative content. This concept is a good way to enhance the knowledge.thanks for sharing
    SQL Azure Online Training
    Azure SQL Training
    SQL Azure Training

    ReplyDelete
  8. Thanks for Sharing This Article.It is very so much valuable content. I hope these Commenting lists will help to my website
    top sailpoint online training

    ReplyDelete
  9. Thanks for Sharing This Article.It is very so much valuable content. I hope these Commenting lists will help to my website
    microservices online training
    best microservices online training
    top microservices online training

    ReplyDelete
  10. Thanks, Experience with various technologies and businesses this is generally helpful selenium online training

    ReplyDelete
  11. Great work. Your article was very helpful. Thanks for sharing valuable points.
    Workday HCM Online Training
    Workday HCM Training
    Workday HCM Certification

    ReplyDelete
  12. Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.

    python training in bangalore

    python training in hyderabad

    python online training

    python training

    python flask training

    python flask online training

    python training in coimbatore


    ReplyDelete
  13. An overwhelming web journal I visit this blog, it's unfathomably amazing. Unusually, in this present blog's substance made inspiration driving truth and reasonable. The substance of data is enlightening.


    Full Stack Course Chennai
    Full Stack Training in Bangalore

    Full Stack Course in Bangalore

    Full Stack Training in Hyderabad

    Full Stack Course in Hyderabad

    Full Stack Training

    Full Stack Course

    Full Stack Online Training

    Full Stack Online Course



    ReplyDelete
  14. Am really impressed about this blog because this blog is very easy to learn and understand clearly.This blog is very useful for the college students and researchers to take a good notes in good manner,I gained many unknown information.


    Data Science Training In Chennai

    Data Science Online Training In Chennai

    Data Science Training In Bangalore

    Data Science Training In Hyderabad

    Data Science Training In Coimbatore

    Data Science Training

    Data Science Online Training

    ReplyDelete
  15. Thanks for provide great informatics and looking beautiful blog, really nice required information & the things i never imagined and i would request, wright more blog and blog post like that for us. Thanks you
    DevOps Training in Chennai

    DevOps Online Training in Chennai

    DevOps Training in Bangalore

    DevOps Training in Hyderabad

    DevOps Training in Coimbatore

    DevOps Training

    DevOps Online Training

    ReplyDelete
  16. Really nice post. Thank you for sharing amazing information.Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
    Java Training in Chennai

    Java Training in Bangalore

    Java Training in Hyderabad

    Java Training
    Java Training in Coimbatore

    ReplyDelete
  17. We provide best Selenium training in Bangalore, automation testing with live projects. Cucumber, Java Selenium and Software Testing Training in Bangalore.
    Online selenium training in India - KRN Informatix is a leading Selenium Training Institute in Bangalore offering extensive Selenium Training.
    Selenium corporate training
    Automation Testing Training in Marathahalli
    Selenium realtime training with projects
    Best Selenium Training in Bangalore
    Selenium Software Training in Bangalore
    Manual testing training in bangalore

    ReplyDelete
  18. Thanks for this amazing blog, visit Ogen Infosystem for creative web design and development services at an affordable price.
    Website Designing Company in Delhi

    ReplyDelete
  19. Thanks for Sharing This Article.It is very so much valuable content. I hope these Commenting lists will help to my website
    blockchain online training
    best blockchain online training
    top blockchain online training

    ReplyDelete
  20. I just loved your article on the beginners guide to starting a blog.If somebody take this blog article seriously
    in their life, he/she can earn his living by doing blogging.Thank you for this article.
    best tibco sportfire online training

    ReplyDelete
  21. I just loved your article on the beginners guide to starting a blog.If somebody take this blog article seriously
    in their life, he/she can earn his living by doing blogging.Thank you for this article.
    top tibco sportfire online training

    ReplyDelete
  22. Water bodies are the main source of transportation for international freight forwarding. Due to this, sea freight company in Delhi,
    visit
    Freight Forwarder in Vietnam
    Shipping Company In India

    ReplyDelete
  23. I just loved your article on the beginners guide to starting a blog.If somebody take this blog article seriously
    in their life, he/she can earn his living by doing blogging.Thank you for this article.
    tibco sportfire online training

    ReplyDelete
  24. I just loved your article on the beginners guide to starting a blog.If somebody take this blog article seriously
    in their life, he/she can earn his living by doing blogging.Thank you for this article.
    tibco sportfire online training

    ReplyDelete
  25. I just loved your article on the beginners guide to starting a blog.If somebody take this blog article seriously
    in their life, he/she can earn his living by doing blogging.Thank you for this article.
    python online training

    ReplyDelete
  26. I just loved your article on the beginners guide to starting a blog.If somebody take this blog article seriously
    in their life, he/she can earn his living by doing blogging.Thank you for this article.
    blueprism online training

    ReplyDelete
  27. Welcome to Softcore Technologies, The Best Training Center for your requirement of Software Programming Courses, Computer Basics, MS Office, Accounting Courses like Tally, Tuition for all subjects starting from 10th Standard (SSC) up to Post Graduation like MBA.

    Best MS Office Training in Hyderabad
    Best Spoken English Training in Hyderabad

    ReplyDelete
  28. Thank you for sharing wonderful information with us to get some idea about it.
    Mulesoft Self Learning
    Mulesoft Online Training in India

    ReplyDelete
  29. Hello, I'm so glad I found your blog, I actually found you by mistake, when I was looking on Google for something else, Anyways, I'm here now and would just like to say thank you for a fantastic post.
    ASP.net Development Company

    ReplyDelete
  30. Great article! It is really helpful for my research. Do post and share more such wonderful content and keep updating.
    Informatica Cloud Training
    SAP Hybris Training
    AWS‌ ‌Data‌ ‌Engineering‌ Training

    ReplyDelete
  31. Really nice blog.And I suggest you the best online training to Become QA Certified Professional QA Training Online

    ReplyDelete
  32. Thanks for Sharing This Article.It is very so much valuable content. I hope these Commenting lists will help to my website Workday HCM Online Training
    best Workday HCM Online Training
    top Workday HCM Online Training

    ReplyDelete