Skip to content

Instantly share code, notes, and snippets.

@avisra
Last active August 18, 2016 23:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avisra/5e52a31b7892b7ec1598 to your computer and use it in GitHub Desktop.
Save avisra/5e52a31b7892b7ec1598 to your computer and use it in GitHub Desktop.
Custom page route handler for improved OutputCache
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Telerik.Sitefinity.Abstractions;
using Telerik.Sitefinity.Web;
using Telerik.Microsoft.Practices.Unity;
using System.Collections.Specialized;
namespace SitefinityWebApp
{
public class CustomPageRouteHandler : PageRouteHandler // Or MvcPageRouteHandler if you have Sitefinity Feather installed
{
protected override bool ApplyServerCache(HttpContextBase context, Telerik.Sitefinity.Modules.Pages.Configuration.OutputCacheProfileElement profile, PageSiteNode siteNode)
{
int duration = profile.Duration;
bool slide = profile.SlidingExpiration;
var cache = context.Response.Cache;
cache.SetCacheability(HttpCacheability.Server);
cache.SetExpires(DateTime.Now.AddSeconds(duration));
cache.SetMaxAge(new TimeSpan(0, 0, duration));
cache.SetValidUntilExpires(true);
if (slide)
{
cache.SetSlidingExpiration(true);
cache.SetETagFromFileDependencies();
}
cache.VaryByHeaders["host"] = true;
// Disable vary by User Agent
cache.VaryByHeaders.UserAgent = false;
// Depending on the page, turn off querystring/postback variation
switch (siteNode.UrlName)
{
case "search":
cache.VaryByParams["*"] = true;
// You could also only vary specific params. Example: cache.VaryByParams["Keywords"] = true;
cache.VaryByParams.IgnoreParams = false;
break;
default:
cache.VaryByParams.IgnoreParams = true;
break;
}
return true;
}
public static void RegisterType()
{
ObjectFactory.Container.RegisterType(typeof(PageRouteHandler), typeof(CustomPageRouteHandler));
}
}
}
@avisra
Copy link
Author

avisra commented Feb 22, 2016

When registering this, you will need to do it in the Bootstrapper.Bootstrapped event. This ensures that it is registered AFTER Feather register's its MvcPageRouteHandler. For instance:

Global.asax.cs

        protected void Application_Start(object sender, EventArgs e)
        {
            Bootstrapper.Bootstrapped += Bootstrapper_Bootstrapped;
        }

        void Bootstrapper_Bootstrapped(object sender, EventArgs e)
        {
                CustomPageRouteHandler.RegisterType();
        }

@OndeVai
Copy link

OndeVai commented Aug 18, 2016

Hi, is this the route handler (MvcPageRouteHandler) for MVC feather widgets only, or MVC pages? Can't seem to get the OutputCache attr to work in my widget controller action methods [OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")], and was wondering if this would be a workaround.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment