Friday, September 28, 2007

ASP.NET caching based on a cookie

You have to use VaryByCustom parameter. Your OutputCache directive will look like this <%@ OutputCache Duration="600" VaryByCustom="StyleSheet" %> and you have declare the following method in which we access the Cookie and pass the value back
 
public override string GetVaryByCustomString(System.Web.HttpContext context, string custom) {
    if (custom == "StyleSheet"){
        return "StyleSheet=" + this.Request.Cookies["StyleSheet"];
    }
}
Given this method, you should be able to cache based not only on cookies, but any other string value or anything that can be converted into a string.

No comments: