Поделиться через


How to: Cache Versions of a Page Using Custom Strings

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 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".

    <%@ 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".

    Response.Cache.SetVaryByCustom("minorversion");
    
    Response.Cache.SetVaryByCustom("minorversion")
    

    Note

    If you attempt to set the custom string both programmatically and declaratively, you will get an InvalidOperationException. You need to choose one approach or the other.

  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.

    <%@ Application language="C#" %>
    <script runat="server">
    public override string GetVaryByCustomString(HttpContext context, 
        string arg)
    {
        if(arg == "minorversion")
        {
            return "Version=" +
                context.Request.Browser.MinorVersion.ToString();
        }
        return base.GetVaryByCustomString(context, arg);
    }
    </script>
    
    <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()
        return base.GetVaryByCustomString(context, arg);
    End Function
    </script>
    

See Also

Tasks

How to: Set the Cacheability of an ASP.NET Page Declaratively

Concepts

Caching ASP.NET Pages

Setting the Cacheability of a Page

Caching Multiple Versions of a Page