Click here to Skip to main content
15,884,672 members
Articles / Web Development / HTML

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

Rate me:
Please Sign up or sign in to vote.
4.71/5 (3 votes)
29 Mar 2014CPOL4 min read 15.8K   164   10  
How to Use ASP.NET Web Optimization Framework in ASP.NET Web Pages Site

Introduction

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.

    Image 1

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

    Image 2

  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:

    Image 3

    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:
    C#
    var bundles = System.Web.Optimization.BundleTable.Bundles;
    
        bundles.Add(new System.Web.Optimization.ScriptBundle("~/bundle/js")
                   .Include("~/Scripts/*.js"));
    
        bundles.Add(new System.Web.Optimization.ScriptBundle("~/bundle/css")
                 .Include("~/Styles/*.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.

    Image 4

  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:
    C#
    @System.Web.Optimization.Scripts.Render("~/bundle/js")
    @System.Web.Optimization.Styles.Render("~/bundle/css")
  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:
    HTML
    <system.web>
        <compilation debug="false" />
    </system.web>
  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.

    Image 5

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.

Image 6

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.

Image 7

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:

Image 8

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.

Image 9

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.

License

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


Written By
Software Developer
India India
I like to code and I really enjoy to share my knowledge with all, Its my passion.

http://abhishekgoswami.com/

Comments and Discussions

 
-- There are no messages in this forum --