ASP.NET 4.0 Output Caching – What is VaryByParam and VaryByCustom

One of the easiest ways to cache ASP.NET pages is using OutputCache page directive. ASP.NET output caching reduces the time needed to execute ASP code by simply saving the output of the ASP in the HTML format inside IIS memory and returning the saved or cached HTML in the future request to the same page. So the future requests to the same ASP page do not require re-execution of the ASP script. In fact the entire ASP engine is totally bypassed and IIS simply returns the snapshot HTML from the memory.

<%@ OutputCache Duration=”300″ VaryByParam=”*” %>

Using this directive, you can take a snapshot of the output of the ASP.NET engine, which is in pure HTML text format, just before it is streamed to the browser from IIS using HTTP protocol. This snapshot is saved in the memory of IIS and served to the browser. Any future calls to this ASP page will result in

simply returning the HTML that has been cached in the IIS memory when the first call was made.

The directive contains two required attributes: Duration and VaryByParam. The Duration attribute specifies how long (in seconds) the data is cached.

What is VaryByParam ?

The VaryByParam attribute allows a page to be cached by a corresponding HTTP parameter; it also allows multiple versions of a page to be cached. (Multiple values are supported; they should be separated by semicolons.)

VaryByParam can be “*” or “None” or  a list of semicolon separated params such as “id;pid;name”

So its easy to cache various versions of the same ASP page by simply specifying VaryByParam=”*”

But  in some cases the output of the ASP page depends on the session or the value of a cookie. In that case how do we distinguish between the various versions of the page based on the cookie? Therese is solution for that, with the VaryByCustom parameter of the OutputCache  directive.

Using VaryByCustom in the OutputCache directive to distinguish between different versions of the ASP page based on the cookie

<%@ OutputCache Duration=”300″  VaryByCustom=”IsAdmin” %>

This is utilised by inserting the code in the Global.asax file. The session id is stored as a cookie, so this is used to retrieve the value for the VaryByCustom attribute. The following C# code shows how this is accomplished:

public override string GetVaryByCustomString (HttpContext context, string arg) {
    if  (arg == "IsAdmin")     {
    HttpCookie cookie = context.Request.Cookies["MyCookie"];
    if (cookie != null) {
         string MemberId = cookie["MemberId "];
         if (MemberId != "" &&  IsAdministrator (int.Parse(MemberId))) {
             return "IsAdmin true";
    return base.GetVaryByCustomString (context, arg);

If you look at the above code, there are two return paths. One returns  ”IsAdmin true” and the other path returns
base.GetVaryByCustomString (context, arg)

So basically it means there are two versions of this page. One that is for Administrators and the other is for regular members. So once the very first regular member accesses this page, it is cached and any furthur regular members will get the cached copy of the regular member version of the page. Also when the first admin requests the page a copy of the Admin version of the page is saved into the cache. Any future requests to the page by other admins simply results in returning the cached copy of the Admin version of the page.

Further Reading

Read my post about Data Caching:  ASP.NET Data Caching – How to use the System.Web.Caching.Cache Object

Also Scott Gu has a good article about output caching: Extensible Output Caching with ASP.NET 4 (VS 2010 and .NET 4.0 Series)

This entry was posted in ASP.NET, Caching and tagged , , , , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

One Comment

  1. frostwire download
    Posted March 17, 2011 at 12:38 pm | Permalink

    trying to find you on facebook, wats your profile

One Trackback

Post a Comment

Your email is never published nor shared. Required fields are marked *


You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>