Monday, September 6, 2010

Control caching based on custom information

In order to use VaryByCustom attribute of OutputCache directive, you must override a method in Global.asax file. Here is a sample code:

public override string GetVaryByCustomString(HttpContext context, string custom)
{
      if (custom == "UserBased" && User.Identity.IsAuthenticated)
      {
            return User.Identity.Name;
      }
      return base.GetVaryByCustomString(context, custom);
}

Then you can set VaryByCustom attribute to “UserBased”. In this example, an instance of the cache will be created for the page based on the user’s identity and username.

0 comments:

Post a Comment