Click here to Skip to main content
15,867,488 members
Articles / Web Development / ASP.NET
Article

Using Multiple Sitemap Files in ASP.NET 2.0

Rate me:
Please Sign up or sign in to vote.
4.78/5 (24 votes)
10 Jan 20063 min read 169.9K   2.5K   64   23
An article on how to use mutliple sitemap files for your site navigation.

Introduction

ASP.NET 2.0 comes with bindable site navigation out of the box in the form of sitemap files. By including a web.sitemap file in the root directory of your website, you can easily bind your navigation control to the sitemap file. But what about placing your sitemap files in app_data (so that they can't be downloaded), or having multiple nav bars and wanting a sitemap file for each of them. This article should give you a good start to overcoming these functionality limitations.

Using the code

The attached project takes the following requirements into consideration. The site has three different areas, a public section for web browsers, a secure section for members, and an administration section. The Secure and Administration sections reside in their own folders so the locations can be secured at a later date. For each section, we have created a master file (located in the masters folder) for consistent look and feel across the section. Each master file will also have its own navigation bar, and will need to bind to its own sitemap file (because the navigation items available in each section will be different). We've placed these sitemap files into the App_Data folder, to prevent them from being downloaded by visitors and spiders.

We now need a way to populate each of these navigation bars with the appropriate sitemap file. This functionality has been abstracted into a user control (UserControls/Menu.ascx), to isolate the logic from the rest of the system.

In the user control, we create a public enum, to represent the different menus available.

C#
public enum SiteMapMenus
{
    Admin, Secure, Public, NotSet
}

I then created a public property to set the menu type at design time.

C#
SiteMapMenus eMenuToLoad = SiteMapMenus.NotSet;
public SiteMapMenus MenuToLoad
{
    get { return eMenuToLoad; }
    set { eMenuToLoad = value; }
}

Now, for the crux of the code. Each menu can accept an XMLDataSource to bind to. The GetMenuDataSource method reads the required sitemap file as an XML file, then creates and returns a data source that can be bound to the control.

C#
XmlDataSource GetMenuDataSource(SiteMapMenus menu, 
                             string serverMapPath)
{
    XmlDataSource objData = new XmlDataSource();
    objData.XPath = "siteMap/siteMapNode";
    switch (menu)
    {
        case SiteMapMenus.Admin:
            objData.DataFile = serverMapPath + @"\App_Data\Admin.sitemap";
            break;
        case SiteMapMenus.Secure:
            objData.DataFile = serverMapPath + @"\App_Data\Secure.sitemap";
            break;
        case SiteMapMenus.Public:
            objData.DataFile = serverMapPath + @"\App_Data\public.sitemap";
            break;
        default:
            break;
    }
    objData.DataBind();
    return objData;
}

We are now almost ready to bind our datasource, but first, as the data source is now XML, and not in the format returned from the sitemap provider, we need to setup our databindings on the menu control itself, as shown below:

XML
<asp:Menu ID="Menu1" runat="server">
    <DataBindings> 
        <asp:MenuItemBinding DataMember="siteMapNode" 
             TextField="title" NavigateUrlField="url"  /> 
    </DataBindings> 
</asp:Menu>

We can now finally bind the source to the control, and this is all fired off in the Page_Load event handler of the User Control.

C#
protected void Page_Load(object sender, EventArgs e)
{
    Menu1.DataSource = GetMenuDataSource(eMenuToLoad, 
                                Server.MapPath("~"));
    Menu1.DataBind();
}

Using our new menu is now as easy as registering the user control on the page, and specifying which menu to display by setting the MenuToLoad property.

XML
<DW:MyMenu ID="MyMenu1" runat="server" MenuToLoad="Secure" />

Something to Note

It is important to point out that for each field specified in your bindings on the menu control, there must be a corresponding item on every node in the sitemap file. For instance, in the attached sitemap files, top level nodes need not have a URL. However, the URL field is still present, and is blank. Removing the URL field from this node will cause a binding exception to be thrown.

I hope you will find this article informative and interesting.

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


Written By
Web Developer
South Africa South Africa
Doug is an Applications Integrator for an online gaming company. He has been programming for 9 years, and has been working with the .NET framework since the beginning of 2003, in both VB.NET & C#.

Comments and Discussions

 
GeneralMy vote of 4 Pin
vsanju20-Oct-11 23:03
vsanju20-Oct-11 23:03 
yet i didn't view the code, but what i need i got the idea from this view, now i definitely go the code, thank u so much
GeneralMy vote of 5 Pin
jamesCompnet4-Sep-10 5:07
jamesCompnet4-Sep-10 5:07 
GeneralMy vote of 1 Pin
jarajeshwaran22-Oct-09 19:35
jarajeshwaran22-Oct-09 19:35 
RantRe: My vote of 1 Pin
TexasMensch27-Jan-10 4:17
professionalTexasMensch27-Jan-10 4:17 
GeneralThanks a lot! Pin
Jules_Joan17-Jan-09 12:28
Jules_Joan17-Jan-09 12:28 
GeneralCheck this guy's blog - seems easier Pin
wgcampbell19-Nov-07 7:03
wgcampbell19-Nov-07 7:03 
GeneralRe: Check this guy's blog - seems easier Pin
Jamie Nordmeyer6-Aug-08 12:27
Jamie Nordmeyer6-Aug-08 12:27 
GeneralMenu does not appear Pin
Prop Top11-Oct-07 4:06
Prop Top11-Oct-07 4:06 
Generalitem.Selected DOES NOT WORK Pin
GreekShowPony13-Mar-07 20:18
GreekShowPony13-Mar-07 20:18 
QuestionIs this possible?? Pin
jfoju14-Feb-07 17:08
jfoju14-Feb-07 17:08 
QuestionCan we use the same way for implmentation in the SiteMapPath control. Pin
tmiku19-Dec-06 23:16
tmiku19-Dec-06 23:16 
GeneralXPath expression Pin
Niyazi Yarar10-Dec-06 4:08
Niyazi Yarar10-Dec-06 4:08 
QuestionMenu User Control Pin
kiwimah6-Sep-06 13:40
kiwimah6-Sep-06 13:40 
AnswerRe: Menu User Control Pin
YosefDov3-Oct-06 2:59
YosefDov3-Oct-06 2:59 
QuestionRe: Menu User Control Pin
manishsawant3-May-07 2:24
manishsawant3-May-07 2:24 
AnswerRe: Menu User Control Pin
kiwimah3-May-07 17:21
kiwimah3-May-07 17:21 
QuestionAlready built in? Pin
McGiv8-Mar-06 3:53
McGiv8-Mar-06 3:53 
AnswerRe: Already built in? Pin
Red Feet6-Oct-06 0:16
Red Feet6-Oct-06 0:16 
GeneralRe: Already built in? Pin
Devinmccloud5-May-07 21:25
Devinmccloud5-May-07 21:25 
GeneralsecurityTrimmingEnabled Feature Pin
soccerchick7-Mar-06 9:40
soccerchick7-Mar-06 9:40 
GeneralVB.net version of the code Pin
sunprana30-Jan-06 23:55
sunprana30-Jan-06 23:55 
QuestionCheck Roles attribute? Pin
Abi Bellamkonda23-Jan-06 16:45
Abi Bellamkonda23-Jan-06 16:45 
AnswerRe: Check Roles attribute? Pin
Doug Wilson24-Jan-06 23:33
Doug Wilson24-Jan-06 23:33 

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

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