Vary Episerver cache by all querystring parameters

I had a Episerver CMS 5 R2 project where there was a lot of templates lifted in from an older solution. It was very querystring intensive so I needed to get away from specifying each querystring-parameter in the httpCacheVaryByParams setting and still enjoy public caching.

Here's how to do it:

While testing I noticed that httpCacheVaryByCustom need to have something different from the default value and httpCacheVaryByParams need to have a dummy value as well. So specifying the following in the siteSettings section of Web.config does the trick:

httpCacheExpiration="0:1:0"
httpCacheability="Public"
httpCacheVaryByCustom="my-custom-string"
httpCacheVaryByParams="needs-a-value-but-not-used"

Then in Global.asax.cs, override GetVaryByCustomString and return RawUrl. This will make the caching routine treat for example /my-list/?page=1 and /my-list/?page=2 as two different documents.

public override string GetVaryByCustomString(HttpContext context, string custom)
{
    return context.Request.RawUrl;
}

Be sure to be logged out and don't run site on a http://localhost URL when testing your implementation.

Published and tagged with these categories: Episerver, ASP.NET, Development