in

Corey Roth and Friends Blogs

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

Cory Robinson's Blog

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.

Published May 07 2008, 02:52 PM by CoryRobinson
Filed under: ,

Comments

 

KyleKelin said:

What is SiteCore?

May 19, 2008 3:10 PM
 

StevenYandell said:

Man that is awesome, you are a lifesaver!

July 24, 2008 9:59 AM
 

Anon said:

context.Request.RawUrl

April 22, 2010 6:58 AM
 

denny said:

This is Denny from infotechguyz.com. I am the creator of this automated employee termination app. Since you run a very informative blog, will you be willing to write a review for this application on your blog?

www.infotechguyz.com/.../ZTermEmployeeTerminationApp.html

This application allows IT administrators to automate common tasks when an employee leaves the company. Usually, IT administrators use multiple consoles and perform variety of tasks to terminate user accounts. This tool allows IT administrator to automate:

Disable Active Directory account

Hide user from Exchange / Outlook Global address list

Forwards email to manager

Reset user's password

Enter notes in Active Directory

And more.

Thanks

Denny

August 13, 2011 9:29 PM

Leave a Comment

(required)
(optional)
(required)
Add
2019.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems