VaryByCustom Caching in asp.net mvc3


It allows you to define custom key/value options to vary your caching by. We can do this by overriding GetVaryByCustomString in your Global.asax.cs file.

This function will be called any time VaryByCustom is called. ASP.NET's cache will look at the string returned by this function and if it matches a value already in its cache, that is the value it will use. In the example below, if custom has "Browser" as the VaryByCustom value and the user's browser is "Mozilla" then it will return the string "Mozilla" as the value.
public override string GetVaryByCustomString(HttpContext context,  string custom)
{
if(custom=="Browser")
{
If(context.Request.Browser.Mozilla) { return "AOLMozilla}
else { return "NotAOL"; }
}
return Guid.NewGuid().ToString();
}


One way to insure this is to return a GUID in string form if none of your custom strings was matched,
so our view won't be cached if it isn't supposed to be cached.
Ebook Download
View all
Learn
View all