Last active
August 18, 2016 23:12
-
-
Save avisra/5e52a31b7892b7ec1598 to your computer and use it in GitHub Desktop.
Custom page route handler for improved OutputCache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.