Magnus Rahl
Jul 27, 2010
(0 votes)

Redirecting container page type

I often find it is necessary to use pages just to structure other pages in a site. Many of you have probably at one time or another had a page in your web site root called [Footer menu] or something similar. Editors do this too, to group or tuck away a number of pages that, for example, don’t fit into the menu structure.

Generally you want those container pages to show as little as possible. If some visitor end up on such a page, for example by erasing the last part of the URL when looking at if there were such a page, they would most likely be looking at a pretty empty (ugly, boring…) page.

Using the built-in redirect

So you say “hey, just use the built-in feature to redirect the container to some useful page”. Sure, that works fine. But it’s likely editors will forget to do it from time to time. A different approach is to create a special container page type, and build the redirect mechanism into the page template. I think editors are more likely to remember to use the special page type when creating a container.

The redirecting container page type

I have constructed several different variants of this page type. This is a simple one.


   1: <%@ Page language="c#" Inherits="Acme.Web.Templates.Pages.RedirectingContainer" Codebehind="RedirectingContainer.aspx.cs" %>
   2: <html>
   3:     <head runat="server">
   4:         <title>Redirecting...</title>
   5:     </head>
   6:     <body>
   7:         In view mode this page redirects to <asp:HyperLink runat="server" ID="hlRedirect" />
   8:     </body>
   9: </html>


   1: public partial class RedirectingContainer : TemplatePage
   2: {
   3:     private PageData _redirectTarget = null;
   5:     protected override void OnInit(EventArgs e)
   6:     {
   7:         base.OnInit(e);
   8:         // Detect edit mode by checking for underscore (workpage prefix) in id
   9:         if (!(Request.QueryString["id"] ?? String.Empty).Contains("_"))
  10:         {
  11:             // Perform redirect immediately
  12:             Response.Redirect(RedirectTarget.LinkURL);
  13:         }
  14:     }
  16:     protected override void OnLoad(EventArgs e)
  17:     {
  18:         base.OnLoad(e);
  19:         // Display a link to the redirect page
  20:         hlRedirect.NavigateUrl = RedirectTarget.LinkURL;
  21:         hlRedirect.Text = String.Format("{0} ({1})", RedirectTarget.PageName, RedirectTarget.PageLink.ID);
  22:     }
  24:     protected PageData RedirectTarget
  25:     {
  26:         get
  27:         {
  28:             if (_redirectTarget == null)
  29:             {
  30:                 // Get specified page or parent if empty
  31:                 PageReference pageLink = (PageReference)(CurrentPage["ContainerRedirectTarget"] ?? PageReference.EmptyReference);
  32:                 // Make sure the redirect target is not the current page (avoid infinite loop)
  33:                 pageLink = (PageReference.IsNullOrEmpty(pageLink) || pageLink.Equals(CurrentPage.PageLink)) ? CurrentPage.ParentLink : pageLink;
  34:                 _redirectTarget = DataFactory.Instance.GetPage(pageLink);
  36:             }
  37:             return _redirectTarget;
  38:         }
  39:     }
  40: }

If the page is viewed in edit mode it does not redirect, it merely displays a message (in the form of a link) to where it would redirect in view mode. If the redirect target property is not set it simply redirects to it’s parent.


Some variations of the concept I have used:

  • Make the redirect target property mandatory. Even simpler, but might confuse editors because they feel they must find something meaningful to redirect it to.
  • Use a (possibly configurable) delay before redirecting, and display a “you are now being redirected to…” message. This gives your framework and it’s visitor statistics scripts etc. the possibility to load. The delayed redirect can be accomplished by the meta http-equiv="refresh" directive with delay and url parameters.
  • Use PropertyUrl rather than PropertyPageReference for the redirect property. This obviously makes it possible to redirect to pages outside the site, but beware that it makes it more difficult to detect infinite loops.
Jul 27, 2010


Please login to comment.
Latest blogs
What’s next after Google Optimize’s sunsetting?

Google has announced that it is sunsetting the Google Optimize and Optimize 360 services, forcing customers to explore new platforms and invest in...

Ynze | Jan 31, 2023 | Syndicated blog

What’s next after Google Optimize’s sunsetting?

Google has announced that it is sunsetting the Google Optimize and Optimize 360 services, forcing customers to explore new platforms and invest in...

Ynze | Jan 31, 2023 | Syndicated blog

Migrating from Providers to CMS 12 ASP.NET Identity with cookie tweaks

Notes on migrating a multi-site from Membership and Role Providers to ASP.NET Identity and changing cookie options dynamically.

Johan Kronberg | Jan 30, 2023 | Syndicated blog

Integrating generative AI in Optimizely CMS: A quick test with OpenAI

Some of the new AI services have received a lot of attention recently. Can you integrate them in Optimizely CMS? Of course, you can!

Tomas Hensrud Gulla | Jan 30, 2023 | Syndicated blog

When best practice isn't the best - Dependency Injection and Optimizely CMS

Some people live and breath 'best practice' development. I am not one of them. Risk is, in-experienced developers (or sometimes experienced) might...

Allan Thraen | Jan 29, 2023 | Syndicated blog

Implementing EmbeddedLocalization in Optimizely CMS 12

My previous post on translation (Translating Optimizely CMS 12 UI components) gives an overview of how to implement the FileXmlLocalizationProvider...

Eric Herlitz | Jan 27, 2023 | Syndicated blog