Click here to Skip to main content
Click here to Skip to main content
Go to top

A Better SiteMapResolve

, 1 Mar 2006
Rate this:
Please Sign up or sign in to vote.
Describes how to create a more useful SiteMapResolve event.

Introduction

I've been working on an ASP.NET 2.0 website recently, and have used the new sitemap features to implement menu and breadcrumb features. However, I had need to dynamically modify the sitemap structure for a specific page.

Others who have tried this will likely be aware of the SiteMap.SiteMapResolve event, which can be used to dynamically construct a sitemap node. However, this event has a rather annoying design flaw - it is static. As such, attaching an event handler to it will result in that handler being called on every request (actually, for every request where the SiteMap.CurrentNode property is accessed).

This article presents a base class for ASP.NET 2.0 pages that provides a page-specific SiteMapResolve event. Pages that inherit from this base class can simply hook into this event and freely modify the sitemap structure knowing that these changes will only affect the current request for that page.

Using the code

Before you go any further, run the example project and take particular note of the following:

  • Pages such as "Contact Us" and "FAQs" are listed in the sitemap file.
  • The NewsItem.aspx page is not listed in the sitemap file. Therefore, it does not appear in the menu. There are links on the home page to three news items, which is the easiest way to get to the NewsItem.aspx page.
  • When you view a news item, an intuitive breadcrumb is displayed - this breadcrumb is a result of dynamically constructed sitemap nodes.

The most important type is the PageBase class. This class inherits from System.Web.UI.Page and adds a SiteMapResolve event that fires only for that page. To use the class, just declare your page to inherit from it as follows:

public partial class NewsItem : Support.PageBase

Once you've inherited from PageBase, you can hook into the SiteMapResolve event in the usual fashion:

protected override SiteMapNode OnSiteMapResolve(SiteMapResolveEventArgs e)
{
    //construct and return your custom SiteMapNode 
    //here - see download for example code
}

Above, I mentioned that the PageBase.SiteMapResolve event fires only for the specific page in which it is handled. That's not entirely accurate. The PageBase class uses a helper method called IsSamePage to determine whether the event should fire. By default, this method returns true if the requests map to the same physical path and have the same query string. This will be sufficient in most cases, but the method can be overridden and customized if necessary.

It's interesting to note that the default implementation of IsSamePage does not actually differentiate between separate instances of pages. That is, if multiple requests for the same page are processed concurrently, you may end up handling the event on behalf of another instance of your page. This won't matter as long as your sitemap structure is based entirely on the information taken from the query string. That's because different query strings will result in IsSamePage returning false and, therefore, the event won't fire.

If your handling of SiteMapResolve relies on some other information, be sure to override IsSamePage accordingly. For example, if you rely on session information to construct your sitemap structure, you could override IsSamePage as follows:

protected virtual bool IsSamePage(HttpContext context1, HttpContext context2)
{
    if (base.IsSamePage(context1, context2))
    {
        return context1.Session["myKey"] == context2.Session["myKey"];
    }
    
    return false;
}

With this override in place, the PageBase.SiteMapResolve event will only fire if:

  • The request is for the same physical file as the current page.
  • The request has the same query string as the current page.
  • The session value with key "myKey" matches in both sessions.

Summary

The code presented in this article will allow you to more easily construct a dynamic sitemap structure in your ASP.NET 2.0 pages. Feel free to copy the PageBase code into your own project, and modify it if necessary. You may already have a base page in place for your project, in which case you can simply copy the relevant code into your base page.

History

  • 1st of March, 2006: Initial revision.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Kent Boogaart
Web Developer
Australia Australia
No Biography provided

Comments and Discussions

 
GeneralVB.NET Example Pinmemberpmlarlfs11-Sep-08 10:25 
Generaldifferent approach proposal Pinmemberwzychla29-Apr-08 23:29 
GeneralOnly works about 90% of the time on a busy site Pinmemberjgd1234521-Apr-08 5:44 
QuestionRe: Only works about 90% of the time on a busy site PinmemberMr. McBoinknuts13-Oct-08 9:07 
GeneralDownload link is broken.. PinmemberCihan16-Dec-07 16:32 
Generalvb version Pinmemberauxcom13-Dec-07 1:15 
GeneralPageBase and Resolve event vs override PinmemberAliasElias9-Aug-07 4:43 
GeneralRe: PageBase and Resolve event vs override PinmemberKent Boogaart9-Aug-07 13:48 
Generalsitemapresolve VB call Pinmemberquineman12-Apr-07 22:52 
GeneralUse Context.CurrentHandler Pinmemberchadly694-Dec-06 19:24 
Generalthanks a lot PinmemberOskars Podans14-Jun-06 21:48 
GeneralRe: thanks a lot PinmemberKent Boogaart14-Jun-06 22:32 
QuestionVB version Pinmembertrancehead13-Mar-06 22:07 
AnswerRe: VB version PinmemberKent Boogaart13-Mar-06 22:22 
GeneralRe: VB version Pinmembermillen20-Aug-06 15:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140905.1 | Last Updated 1 Mar 2006
Article Copyright 2006 by Kent Boogaart
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid