Monday, January 21, 2013

Caching with Specific Query String Parameters in ASP.NET


Previous post described about the caching and query string in ASP.NET. This post describes how to caching with specific query string parameters.

Setting VaryByParam to the wildcard asterisk (*) is unnecessarily vague. It’s usually better to specifically identify an important query string variable by name. Here’s an example:
<%@ OutputCache Duration="20" VaryByParam="ProductID" %>
In this case, ASP.NET will examine the query string, looking for the ProductID parameter. Requests with different ProductID parameters will be cached separately, but all other parameters will be ignored. This is particularly useful if the page may be passed additional query string information that it doesn’t use. ASP.NET has no way to distinguish the “important” query string parameters without your help.

You can specify several parameters as long as you separate them with semicolons:
<%@ OutputCache Duration="20" VaryByParam="ProductID;CurrencyType" %>
In this case, ASP.NET will cache separate versions, provided the query string differs by ProductID or CurrencyType.

No comments:
Write comments
Recommended Posts × +