Configure cache variation by query string

By default, the Sitefinity CMS output cache does not vary by query string parameters. This means that if a cache version of the page is found, Sitefinity CMS will serve it, regardless of the query string portion f the page URL. 

If you are using query strings in your custom code, and would like to have the page cache vary by the query string parameters you must configure this explicitly. Sitefinity CMS provides two ways of handling this:

  • You can manually configure a list of parameters for which output cache should vary, using the varyByParams output cache header. For more information see the Configure cache headers for page and media cache profiles section in the Configure cache profiles article.
  • You can use the  Sitefinity CMS QueryStringGet extension method to read the query string values in your code. When Sitefinity CMS initializes, it scans all assemblies for usages of QueryStringGet and automatically registers output cache variations for the query string parameters passed as arguments to QueryStringGet. If you have already registered cache variation for any parameters  in your Sitefinity CMS output cache profile, using QueryStringGet does not override them, but adds them dynamically to the existing list.

    NOTE: There is no need to use QueryStringGet in the cases where you have disabled output cache for the page, or have configured varyByParams="*" in your output cache profile.

Using the QueryStringGet extension method in your code

If you want Sitefinity CMS to automatically register output cache variations for the query string parameters you use, you should substitute usages of the standard HttpRequest.QueryString[key] ASP.NET approach with the Sitefinity CMS QueryStringGet() extension method from the Telerik.Sitefinity.Web namespace . The method accepts the query string parameter name as argument, so you can easily switch to this approach. The following example demonstrates usage of the QueryStringGet extension method:

Using CacheVariationParamValidator to register cache variations for query string parameter values

In addition to automatically registering cache variations for your query string parameters, the QueryStringGet extension method enables you to use a CacheVariationParamValidator. The CacheVariationParamValidator provides a mechanism to register cache variations for the query string parameter values.  Inside the validator logic you can specify for which values of the query string parameter Sitefinity CMS should serve a new version of the page. This mechanism is very useful in case you want to enable cache variations for certain query string parameters, but would like to limit the values for which Sitefinity CMS creates a new cached version of the page.

To use the validator mechanism you must create a class and inherit from the CacheVariationParamValidator abstract class. Inside your class you must implement the Validate method, which accepts the query string parameter value as argument. Inside your Validate method logic you can compare the parameter value against a predefined list of allowed values. Finally, you must specify the type of your validator, when using the generic overload of the QueryStringGet extension method. For example:

Increase your Sitefinity skills by signing up for our free trainings. Get Sitefinity-certified at Progress Education Community to boost your credentials.

Web Security for Sitefinity Administrators

The free standalone Web Security lesson teaches administrators how to protect your websites and Sitefinity instance from external threats. Learn to configure HTTPS, SSL, allow lists for trusted sites, and cookie security, among others.

Foundations of Sitefinity ASP.NET Core Development

The free on-demand video course teaches developers how to use Sitefinity .NET Core and leverage its decoupled architecture and new way of coding against the platform.

Was this article helpful?