JavaScript Editor jscript editor     Web designer 



Main Page

In addition to varying the output cache by browser type and parameters, you can cache multiple versions of page output based on different strings returned by a method that you define.

When you cache pages based on custom strings, you first specify an identifier for the custom string to use. You then create a method in the application's Global.asax file that accepts the identifier and returns a value to vary the output cache by.

To cache multiple versions of page output based on custom strings

  1. In the ASP.NET page, include an @ OutputCache directive with the required OutputCacheParameters.Duration and VaryByParam attributes. The Duration attribute must be set to an integer greater than zero. If you do not want to use the functionality provided by the VaryByParam attribute, you must set its value to "None".

  2. To set the custom string declaratively, in the @ OutputCache directive, include the VaryByCustom attribute that is set to the string that you want to vary the output cache by.

    The following directive varies the page output by the custom string "minorversion".

    В CopyCode imageCopy Code
    <%@ OutputCache Duration="10" VaryByParam="None" VaryByCustom="minorversion" %>
  3. To set the custom string programmatically, call the SetVaryByCustom method and pass it the custom string to use.

    The following code example shows how to set the custom string to "minorversion".

    C#В CopyCode imageCopy Code
    Response.Cache.SetVaryByCustom("minorversion");

    Visual BasicВ CopyCode imageCopy Code
    Response.Cache.SetVaryByCustom("minorversion")
  4. In the application's Global.asax file, override the GetVaryByCustomString method to specify the behavior of the output cache for the custom string.

    As its arg parameter, the overridden method accepts the string that you set in the VaryByCustom attribute or in the SetVaryByCustom method. For example, you might have pages that are cached by the minor version of the requesting browser. For these pages you can set the VaryByCustom attribute to "minorversion". Then, in the overridden GetVaryByCustomString method, you can check the arg parameter and return different strings depending on whether the value of the arg parameter is "minorversion".

    The following code example shows a Global.asax file with an override of the GetVaryByCustomString method.

    C#В CopyCode imageCopy Code
    <%@ Application language="C#" %>
    <script runat="server">
    public override string GetVaryByCustomString(HttpContext context, 
        string arg)
    {
        if(arg == "minorversion")
        {
            return "Version=" +
                context.Request.Browser.MinorVersion.ToString();
        }
       else
       {
          return "";
        }
    }
    </script>

    Visual BasicВ CopyCode imageCopy Code
    <script runat="server">
    Public Overrides Function GetVaryByCustomString(context _
            As HttpContext, arg As String) As String
        If (arg = "minorversion") Then
            Return "Version=" & _
                context.Request.Browser.MinorVersion.ToString()
        Else
            Return ""
        End If
    End Function
    </script>

See Also



JavaScript Editor jscript editor     Web designer