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 standardHttpRequest.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:

C#
using System.Web;
using Telerik.Sitefinity.Web;

namespace SitefinityWebApp
{
   public class QueryStringCacheVariationSamples
   {
       public string ReturnQueryStringValue()
       {
           var request = HttpContext.Current.Request;
           //Using QueryStringGet automatically creates a cache variation for this parameter name
           //This way, Sitefintiy CMS knows to serve a different version of the page when the queryString is present in the page URL
           var colorQueryParameterValues = request.QueryStringGet("color");
           return colorQueryParameterValues;
       }
   }
}

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:

C#
using System;
using System.Linq;
using System.Web;
using Telerik.Sitefinity.Web;

namespace SitefinityWebApp
{
   public class QueryStringCacheVariationValidatorSamples
   {
       public string ValidateAndReturnQueryStringValue()
       {
           var request = HttpContext.Current.Request;
           //You can pass additional arguments to your validator in case you need to implement additional logic
           var validatorArguments = new string[] { };
           //Using QueryStringGet with validator parameter creates a cache variation for this parameter name
           //and enables you to implement validation logic where you can specify for which values of the queryString Sitefintiy CMS should serve a different version of the page 
           var colorQueryParameterValues = request.QueryStringGet<ColorQueryParameterValidator>("color");
           return colorQueryParameterValues;
       }
   }

   [Serializable]
   internal class ColorQueryParameterValidator : CacheVariationParamValidator
   {
       protected override bool Validate(string paramValue, string[] arguments)
       {
           var allowedValues = new string[] { "red", "green", "blue" };
           return allowedValues.Contains(paramValue);

       }
   }
}
Want to learn more?
Enhance your Sitefinity skills by enrolling in free training sessions. Become Sitefinity certified through Progress Education Community to strengthen your professional credentials.