The VaryByCustom attribute uses either the browser type or other custom strings to output cache multiple versions of a page.
When you include <%@ OutputCache Duration=300 VaryByCustom = "browser" %>
at the top of your page, you instruct the output cache to cache varying versions of the page according to the information provided by the page’s Request.Browser.Type property ( browser name and major ID ). If an Internet Explorer browser and a Netscape Navigator 4.0 browser each request a page that you include this directive in, an entry is added for each in the output cache.
You can also use this attribute to extend the functionality of the output cache. To do this, you must include a custom string in the VaryByCustom attribute in the @ OutputCache, and then override the HttpApplication.GetVaryByCustomString method in your application’s Global.asax file. The code that you write in your override statement defines what the output cache varies by.
In the following directive, the custom string is defined as Frames
.
<%@ OutputCache Duration=300 VaryByCustom = "Frames" %>
In your application’s Global.asax file, you can include the following code to specify to the output cache whether the requesting browser supports frames, and if it does, instruct it to store a version of the page output in the cache.
public override string GetVaryByCustomString ( HttpContext context, string arg )
switch ( arg )
case "Frames":
return "Frames = " + context.Request.Browser.Frames;
case "JavaScript":
return "JavaScript = " + context.Request.Browser.JavaScript;
default:
return "";
}
}
Public Overrides Function GetVaryByCustomString ( context As HttpContext, arg As String ) As String
Select Case arg
Case "Frames"
Return "Frames = " & context.Request.Browser.Frames
Case "JavaScript"
Return "JavaScript = " & context.Request.Browser.JavaScript
Case Else
Return ""
End Select
End Function |
|
C# |
VB |
Caching Portions of an ASP.NET Page Caching Application Data