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 www.site.com/Footer/Contacts 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.
Template:
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>
Codebehind:
1: public partial class RedirectingContainer : TemplatePage
2: {
3: private PageData _redirectTarget = null;
4:
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: }
15:
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: }
23:
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);
35:
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.
Variations
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.
Comments