Add custom cache headers
This article demonstrates how to add custom cache headers in addition to the Sitefinity CMS default ones. For more information, see Advanced settings of cache profiles.
You do this by adding the setNoTransform cache header in a specific cache profile:
- Navigate to Administration » Settings » Advanced » System » Output cache settings » Page Cache profiles.
- Choose profile and open the Parameters page.
- Click Add new.
- In the Key field, enter
setNoTransform. - Set the value to
true.
Next, you add a CustomPageOutputCacheStrategy in your project. Use the following code:
C#
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using Telerik.Sitefinity.Web;
namespace SitefinityWebApp
{
public class CustomPageOutputCacheStrategy : PageOutputCacheStrategy
{
public override bool ApplyServerCache(HttpContextBase context, Telerik.Sitefinity.Modules.Pages.Configuration.IOutputCacheProfile profile, PageSiteNode page)
{
var baseCacheSettings = base.ApplyServerCache(context, profile, page);
var cache = context.Response.Cache;
//check for setNoTransform parameter
if (PageOutputCacheStrategyHelper.ContainsKey(profile.Parameters, "setNoTransform"))
{
if (profile.Parameters.Get("setNoTransform") == "true")
{
cache.SetNoTransforms();
}
}
return baseCacheSettings;
}
}
}
In the code above, you use the PageOutputCacheStrategyHelper. You implement it in the following way:
C#
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
namespace SitefinityWebApp
{
public static class PageOutputCacheStrategyHelper
{
public static bool ContainsKey(this NameValueCollection @this, string key)
{
return @this.Get(key) != null
|| @this.Keys.Cast<string>().Contains(key, StringComparer.OrdinalIgnoreCase);
}
}
}
Finally, register the CustomPageOutputCacheStrategy in the Global.asax file:
C#
using Telerik.Sitefinity.Abstractions;
using Telerik.Sitefinity.Data;
using Telerik.Microsoft.Practices.Unity;
using Telerik.Sitefinity.Services;
protected void Application_Start(object sender, EventArgs e)
{
SystemManager.ApplicationStart += SystemManager_ApplicationStart;
}
private void SystemManager_ApplicationStart(object sender, EventArgs e)
{
ObjectFactory.Container.RegisterType<PageOutputCacheStrategy, CustomPageOutputCacheStrategy>(new ContainerControlledLifetimeManager());
}
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.
Get started with Integration Hub | Sitefinity Cloud
This free lesson teaches administrators, marketers, and other business professionals how to use Sitefinity Integration Hub to create automated workflows between Sitefinity and other business systems.
Web Security for Sitefinity Administrators
This free lesson teaches administrators the basics about protecting your Sitefinity instance and your sites from external threats. 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 ASP.NET Core and take advantage of its decoupled architecture and modern development model.