Click here to Skip to main content
12,697,076 members (26,681 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


10 bookmarked

How to Use ASP.NET Web Optimization Framework in ASP.NET Web Pages Site

, 29 Mar 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
How to Use ASP.NET Web Optimization Framework in ASP.NET Web Pages Site


In my previous article, I described what is ASP.NET Web Optimization Framework and what classes are provided by this framework in order to use this framework in web form, web pages and ASP.NET MVC. So here in this article, I will explain how to use this framework in ASP.NET web page site.

Since I have already explained about ASP.NET Web Optimization framework, I am directly switching to implementation. But I will recommend before reading this article to please go through my previous article because that is a prerequisite for this article.

So now, let’s see how we can use this framework in a web pages site step by step.

  1. Create an ASP.NET website (Razor 2) Web Site.

  2. Now you can see solution explorer like the following:

  3. One important thing here is that this template is already installed with Web Optimization Framework so you can check in bin folder required DLLs have already been added.
  4. Now you need to create two folders, Scripts and Styles for keeping scripts and CSS files. Now your Solution explorer should look like following:

    Here I have added 2 files in scripts folder and 2 files in Styles for demonstration. You can add as per your requirement.

  5. Open _AppStart.cshtml file. You can see that there is already some code present in this file and some code is commented so there is no need to worry about that. We need to write our own code. You need to write the following code inside this file:
    var bundles = System.Web.Optimization.BundleTable.Bundles;
        bundles.Add(new System.Web.Optimization.ScriptBundle("~/bundle/js")
        bundles.Add(new System.Web.Optimization.ScriptBundle("~/bundle/css")

    Here, I am directly creating and adding two bundles into bundle table. You can define according to your classification and use. You can further classify a single bundle into other sub bundles. One more thing you might have noticed here is that I am using *.js, because I am adding all js files into one bundle and same for CSS. So exactly what I did here, I bundled all JavaScript files into one bundle, it means they will load as single entity not as multiple different files and same for CSS, I created a single bundle for all CSS files.

    I think now you can better understand practically how this framework optimizes the calls and loading of resources.

  6. Add one .cshtml for consuming these bundles. So for adding cshtml file, right click on solution explorer, click on add and choose new item, then you will get one dialog box. Now you can choose Empty Page(Razor v2) and named file as Welcome.cshtml.

  7. Now, we are ready with our bundle so the last task is to consume this bundle into our file. That we can do by using Scripts.Render and Styles.Render methods as follows:
  8. Now that we are ready with all the implementation, only one task is remaining, i.e., enabling the bundling and minification. As I have described in my previous article, there are two ways to enable it. So you can use either of the ways. Here I am enabling by setting web.config file’s compilation elements’s debug attribute as follows:
        <compilation debug="false" />
  9. So now, it is time to be happy and test our work. So run the Welcome.chstml page and see you will get the following screen.

Now press F12 to see the real magic of web optimization framework.

Click on Script tab and select Test.aspx dropdown list. You can see js?v…….. like some random string that is the bundle name.

Here, if you have observed that in place of separate js files, only one bundle is loading. You can see the real calling for the different resources into Network tab. Here you can see, there is only one call for JavaScript files and one for CSS files.

If you have not seen this developer tool of Internet Explorer and you did not observe here for normal application without ASP.NET Optimization Framework, then you might not be able to differentiate the real one, so for those people I can show you the proper difference.

Now just go back in your web.config file and set debug = true and run the application and again press F12. Now, you will get the following screen when you click on Script tab:

If you can observe here, now you can see two separate JavaScript files are loading separately. So suppose you have added 20 js files in your view, then there are 20 separate calls which will be made for 20 js files. Now just check in network tab so here you can see there are 4 separate calls for 4 separate files.

Now I think you can better understand the use of ASP.NET Web Optimization framework. So here in this article, I tried to cover all the basics of how to use ASP.NET web optimization framework in your web page website. Thanks for reading.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Abhishek Kumar Goswami
Software Developer
India India
I like to code and I really enjoy to share my knowledge with all, Its my passion.

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 29 Mar 2014
Article Copyright 2014 by Abhishek Kumar Goswami
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid