Export (0) Print
Expand All

Caching Versions of a Page, Based on Parameters

If you use GET and POST HTTP methods to gather information from users, you know that they can generate different responses depending on user input. GET requests with query strings and POST requests are associated with HTML forms, with which you can gather information from users. You can cache multiple versions of page responses, based on these parameters. To do this, you can use the @ OutputCache directive's VaryByParam attribute, or the HttpCachePolicy.VaryByParams property.

To cache multiple versions of page output declaratively, based on query string or form POST parameters

  1. Include an @ OutputCache directive in the .aspx file that you want to cache. Be sure to set the required Duration attribute to a value greater than zero.
  2. Use the VaryByParam attribute to set the query string or form POST parameter that you want to vary the page by. By including the following example at the top of an .aspx file, different versions of page output will be cached for each request that arrives with a different City parameter value.
    <%@ OutputCache duration="60" varybyparam="City" %>
    
    Note   If you want to vary the output cache by multiple parameters, define them in the VaryByParam attribute by using a semicolon-delimited list. If you want vary the cache by all the parameter values, set the attribute to an asterisk (*).

To cache multiple versions of page output programmatically, based on query string or form POST parameters

  1. In the page's code-declaration block or code-behind class, use Response.Cache syntax to set the expiration and visibility policies for the cached page content. You can accomplish this using the HttpCachePolicy.SetExpires and HttpCachePolicy.SetCacheability methods, respectively.
  2. In the same code, specify the parameter name as the argument for the VaryByParams property and set the property to true. The following code caches multiple versions of a page when requests arrive at the server with different City parameter values.
    Response.Cache.VaryByParams["City"] = true;
    [Visual Basic]
    Response.Cache.VaryByParams("City") = true
    
    Note   If you want to vary the output cache by multiple parameters, include them in a semicolon-delimited list in the VaryByParams argument. If you want vary the cache by all the parameter values, set the attribute to an asterisk (*).
    The following example demonstrates varying the page output by
    City
    and
    ZipCode
    parameters.
    Response.Cache.VaryByParams["City;ZipCode"] = true;
    [Visual Basic]
    Response.Cache.VaryByParams("City;ZipCode") = true
    

The following discussion details how versions of a page are cached, depending upon incoming requests with different values that are passed in form POST or query string parameters.

Your application contains an .aspx file, Mycache.aspx, and you specify that different versions of the page should be cached, based on a city parameter. Four requests arrive with query strings attached: two with count parameters and four with city parameters.

http://www.microsoft.com/caching/mycache.aspx?count=10&city=dallas
http://www.microsoft.com/caching/mycache.aspx?count=10&city=newyork
http://www.microsoft.com/caching/mycache.aspx?city=dallas
http://www.microsoft.com/caching/mycache.aspx?city=seattle

Since you instruct the output cache to vary by only the city parameter, three versions of the page's output are cached, one for dallas, one for newyork, and one for seattle. The first request, containing the dallas parameter value, causes a version of the page output to be cached, while the third request, also with the dallas parameter value, obtains the version of the page from the output cache. For the length of time that these versions remain in the cache, requests that contain city parameters with the value of dallas, new york, or seattle are satisfied from the output cache.

If you vary the output cache by both the city and count parameters, four versions of the page output are cached. This is the case because the first entry contains a count query string parameter, while the third entry does not. If you vary the cache by all possible parameters, an output-cache entry is created for each request with unique parameter values. In the current example, this also causes four versions of the page to be cached.

Note   GET query string or form POST parameters with identical pairs of keys and values, regardless of the order that those parameters are passed, are satisfied by the cached version of the page for as long as it has not expired. Since GET query string and form POST parameters are case-sensitive, if the parameter value is cased differently ASP.NET will treat the parameter as different from the original cached item and enter another version of the page in the output cache.

See Also

@ OutputCache | Caching ASP.NET Pages | HttpApplication.GetVaryByCustomString Method | VaryByParams | VaryByHeaders

Show:
© 2014 Microsoft