in

Dot Net Mafia

Group site for developer blogs dealing with (usually) .NET, SharePoint 2013, SharePoint 2010, Office 365, SharePoint Online, and other Microsoft products, as well as some discussion of general programming related concepts.

Cory Robinson's Blog

May 2008 - Posts

  • Varying OutputCache by URL in Sitecore

    I know it has been quite some time since my last blog post, but I thought this was worth writing down.  The goal was to vary our header html caching by URL.  The OutputCache directive in ASP.NET does not provide this option out of the box, so I had to do a VaryByCustom.  VaryByParam is required so we set it to None.  The VaryByCustom value is just an arbitrary key that you come up with.  Here is the OutputCache directive we used for our header user control:

    <%@ OutputCache Duration="60" Shared="true" VaryByParam="None" VaryByCustom="sitecoreurl" %>

    In order to use VaryByCustom, one must provide a GetVaryByCustomString override in Global.asax.   The context parameter can be used to get context.Request.Url, but in Sitecore, this is set to the URL of the Sitecore layout that is used for the content page, and not the actual resulting URL after Sitecore processes it.  So we had to use the FriendlyUrl from the Sitecore item to get the actual result URL for the page.

            public override string GetVaryByCustomString(HttpContext context, string arg)

            {

                if (arg == "sitecoreurl")

                {

                    if (Sitecore.Context.Item != null)

                        return Sitecore.Context.Item.Paths.GetFriendlyUrl();

                    else

                        return string.Empty;

                }

                else

                {

                    return string.Empty;

                }

            }

     

    The result is that our header html caches different versions of the html by URL, so the html for /Products.aspx and /FAQs.aspx would be cached separately.

2015 dotnetmafia.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems