jump to navigation

Caching an ASP.Net page October 6, 2010

Posted by fofo in asp.net, VB 2005, VB 2008, VB 9.0, Visual Basic 10.0, Visual Studio 2005, Visual Studio 2008, Visual Studio 2010, VS 2010.
1 comment so far

The issue of “caching” keeps coming up in my asp.net seminars so I though it would be a good idea to write a post. I know it is a well documented feature and widely used but I really believe most people will be able to use this rather simple example and understand the concepts behind caching.

You can use any of the versions of Visual studio (VS 2005,VS2008,VS2010 express editions work fine).

I will use VS 2010 Ultimate edition and VB.net as the .Net language of choice.

1) Create an Asp.net web site and name it as you want.

2) In the default.aspx page add a new paragraph or header with content of your choice.In my case i have added something like this

<h2>Caching is really useful!!!!</h2>

3) Let’s think simple at first. We do want to cache the entire page (default.aspx)

Just below the Page directive ( top of the page ) you add the following bit of code:

<%@ OutputCache Duration="20" VaryByParam="none" %>

We do cache the page for 20 seconds. That is determined by the Duration Attribute.

4) Save your work and run your application. The first time we see the output of the asp.net page, this is served directly from the asp.net engine.

If you hit refresh for a period for less than 20 seconds the page is served from the Cache.

5) Let’s see a more complex scenario involving query strings and how we can make a decision on whether to cache the page or not based on the value of the query string. Add a new label control to the .aspx page

 <asp:Label ID="mylabel" runat="server"></asp:Label>

In the Page_Load event handler routine add the code

If (Not Request("id") Is Nothing) Then 
mylabel.Text = " The query string value is " + Request("id").ToString()

End If

6) Change the OutputCache directive to

 <%@ OutputCache Duration="20" VaryByParam="id" %>

7) Run your application and type in the url address bar http://localhost:6141/caching/Default.aspx?id=5. See the results. If you hit refresh right away the page is refreshed from the cache and it is not reloaded.

If you type this http://localhost:6141/caching/Default.aspx?id=6 ( within the 20 seconds period ) it senses that the id has changed now and it re-renders the page. It also sets a new caching period of 20 seconds.

8) What is really interesting to bear in mind ( and not many people know ) is that you can have different caching settings depending on the client’s browser.The first step is to change the OutputCache directive to this

<%@ OutputCache Duration="20" VaryByParam="id" VaryByCustom="browser" %>

9) Basically we say “Continue to cache the page as long as the clients use the same browser”. So if you have 3 people in a row that have IE8 hitting your page, will all get the same cached version of the page.

If someone hits our page with Firefox or Chrome, they will a brand new rendered page. At this time it will be created a 20 seconds cache window period specific for each browser.This functionality is built into the caching module, and will insert separate cached versions of the page for each browser name and major version.

10) Now if we need to apply the caching techniques we just learnt in all pages of our asp.net application we will have to make a few modifications in the web.config file.We will add something that is called “Cache Profiles”

Just below the <system.web> add this snippet

          <add name="basic" 




So we have created a new profile that is called “basic”. Now let’s apply this cache profile to one or more pages in our website.

So we switch back to our default.aspx page and in the OutputCache directive, type

 <%@ OutputCache CacheProfile="basic" %>

Run your application again and you will see that we do have the same caching effects.

Obviously you can create different cache profiles and apply them to various pages.

I will have more posts on caching shortly.

Hope it helps!!!

Caching an asp.net page depending on the user’s browser language October 6, 2010

Posted by fofo in VB 2005, Visual Studio 2008, asp.net, VB 2008, Visual Studio 2010.
Tags: ,
add a comment

This is my second post regarding caching in asp.net applications.

A friend of mine asked me if he can control caching of an asp.net page depending on the user’s language browser.

Let’s see how we can accomplish that with a step by step example.

You can use any of the versions of Visual studio (VS 2005,VS2008,VS2010 express editions work fine).

I will use VS 2010 Ultimate edition and VB.net as the .Net language of choice.

We will identify the language of the user’s browser from the HTTP headers.

More specifically we will look at the Accept-Language header item.

1) Create an Asp.net web site and name it as you want.Add a label web server control to your default.aspx page.

2) In the default.aspx just below the Page directive ( top of the page ) you add the following bit of code:

<%@ OutputCache Duration="50" VaryByHeader="Accept-Language"

We want to use VaryByHeader and assign it to the header item Accept-Language that we want to vary caching by.

So now this page is going to be cached based on the language of the user’s browser.

3) Now switch back to the code behind and in the Page_Load event handling routine type the following

If Not (Request.Headers("Accept-Language")) Is Nothing Then 
mylabel.Text = "language of the user is " +
End If

4) Run your application and see the result-your browser’s language. In most cases it will be something like “en-us“, or anything else that the HTML page will output. So if now we have 20 people

hitting our page – within the 50 seconds cache period – and all of them have “en-us” as their browser’s language they are going to get the cached version of the page. The page will not be re-rendered through the whole

page events-lifecycle.

5) If we have people hitting our page with a browser with a different language (.eg fr,es ) the Asp.Net would cache a second version of the page in french. So now we would have 2 cached versions of the page, one in english and one in french.So the first french person (browser language) to hit the page, he would see the page rendered to him ( the whole page lifecycle will run ) and at the same time it will be cached so subsequent requests from french people-users-browsers will be served from the cache.

If you need the code, just email me.

Hope it helps !!!