Apps

Working with ASP.NET site maps


Many of the features added to the .NET Framework with version 2.0 focus on enabling developers to quickly build powerful applications. A good example is a group of controls that allow you to easily include navigation features in an ASP.NET Web site. The foundation of these navigation controls is the Site Map Web server control. This article discusses the Site Map Web server control's design, usage, and interaction with other controls.

Logical structure

Web applications are rarely simple these days, as they often comprise countless pages and have a definite structure. A good example is the TechRepublic site, which has a home page and sections for downloads, forums, blogs, and so forth. The logical structure of a Web site is often called its site map. Once this structure has been defined, you can set up the Web site's navigation structure.

ASP.NET site maps

Beginning with version 2.0, ASP.NET allows you to define an application's structure via the Site Map Web server control. While the Web community has developed a sitemap standard (notice that Microsoft is a contributor), Microsoft uses its own format. The site map is defined in a separate file. The default format for this file is XML, but custom site map providers may be developed to use a particular format.

In this article, I'll use the default XML format and name the site map file Web.sitemap. For a basic site, you should place this file in the application's root directory. However, like web.config files, a site may use multiple site map files. You may easily create this file via a text editor. If you're using Visual Studio, you may add a site map to a Web application by going to the Add New Item and selecting Site Map from the list. The following code demonstrates the format for the site map file added to a Web project:

<?xml version="1.0" encoding="utf-8" ?>

<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >

<siteMapNode  url="" title=""  description="">

<siteMapNode url="" title=""  description="" />

<siteMapNode url="" title=""  description="" />

</siteMapNode>

</siteMap>

If you are familiar with the previously mentioned site map standard, you will notice that the XML namespace URL for an ASP.NET site map is Microsoft specific. The file contains the root SiteMap element, which encompasses the complete site map. Within this root node, SiteMapNode elements define areas of the site. An individual SiteMapNode may contain zero or more child SiteMapNodes; these children may contain zero or more child nodes, and so on.

Each SiteMapNode supports the following attributes:

  • url: The page to which the user will be directed when the item is selected.
  • title: The text displayed to represent the item.
  • description: The tooltip text displayed when the user hovers over the item with his or her mouse.
  • provider: You may specify the site map provider for the item.
  • resourceKey: The resource key used to localize the item.
  • roles: A collection of roles associated with the item. This is used to control access via security trimming.
  • securityTrimmingEnabled: Signals whether security trimming is enabled.
  • siteMapFile: Multiple site map files may be used in an application. A child node can reference another site map file via this attribute.

SiteMapNode elements typically contain three attributes: url, title, and description. The following listing is a site map file for a sample site with only three pages:

<?xml version="1.0" encoding="utf-8" ?>

<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >

<siteMapNode url="Default.aspx" title="Default Page"  description="Home">

<siteMapNode url="Test1.aspx" title="Test Page 1"  description="Only a test" />

<siteMapNode url="Test2.aspx" title="Test Page 2"  description="Still a test" />

</siteMapNode>

</siteMap>

Programmatic access

The System.Web namespace contains the SiteMap class, which corresponds to the ASP.NET site map feature. It is an in-memory representation of the navigation structure for a site. The class provides access to the various elements within a site map. The two most important (or used) properties of the class are CurrentNode and RootNote.

  • CurrentNode: Returns a SiteMapNode object that represents the currently requested page. This property allows you to use the current page as a starting point; a good example is using the CurrentNode to send the user to the parent page of the current page.
  • RootNode: Returns a SiteMapNode object that represents the top-level page of the site navigation structure. You can use the SiteMap class to easily return a user to the root page of a site via this property.

This C# example sends a user to the parent page for the current page via the CurrentNode property of the SiteMap class:

Response.Redirect(SiteMap.CurrentNode.ParentNode.Url);

Both the CurrentNode and RootNode properties of the SiteMap class return instances of the SiteMapNode, class which contains the following properties and methods for traversing a site map tree.

  • HasChildNodes: Signals whether the node has children. HasChildNodes is a Boolean value.
  • NextSibling: Returns the next node in the site map tree on the same level as the current node.
  • PreviousSibling: Returns the previous node in the site map tree on the same level as the current node.
  • RootNode: Returns the root node of the site map.
  • Title: Allows you to populate or read the title assigned to a node.
  • Url: Allows you to populate or read the URL assigned to a node.
  • Description: Allows you to populate or read the description assigned to a node.

This C# code from an ASP.NET page traverses a site map displaying the text and URL for each node when the page is loaded:

protected void Page_Load(object sender, EventArgs e) {

foreach (SiteMapNode cNode in SiteMap.RootNode.ChildNodes){

walkTree(cNode);

} }

protected void walkTree(SiteMapNode pNode){

foreach (SiteMapNode child in pNode.ChildNodes){

if ((child.Title.Length > 0) && (child.Url.Length > 0)){

Response.Write(child.Title.ToString() + " -- " + child.Url.ToString());

} } }

The equivalent VB.NET code follows:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)Handles Me.Load

For Each cNode As SiteMapNode In SiteMap.RootNode.ChildNodes

WalkTree(cNode)

Next

End Sub

Private Sub WalkTree(ByVal pNode As SiteMapNode)

For Each child As SiteMapNode In pNode.ChildNodes

If ((Child.Url.Length > 0) And (Child.Title.Length > 0)) Then

Response.Write(child.Title.ToString() + " -- " + child.Url.ToString())

End If

End Sub

Providing a guide

Laying out the logical structure of a Web application provides a starting point for building user interface navigation controls. ASP.NET 2.0 simplifies the building of such controls with the Site Map Web server control and related navigation controls. The site structure is detailed in a site map file and used to populate navigation elements. In previous versions of ASP.NET, this simple process consumed a chunk of development time.

The site map is the base for using the navigation controls available in ASP.NET 2.0. The site map is used as the data source for the navigation controls. These navigation controls include Menu, SiteMapPath, and TreeView. Next week, I will provide the details of using the SiteMapPath control with a site map.

Tony Patton began his professional career as an application developer earning Java, VB, Lotus, and XML certifications to bolster his knowledge.

---------------------------------------------------------------------------------------

Get weekly .NET tips in your inbox
TechRepublic's free .NET newsletter, delivered each Wednesday, contains useful tips and coding examples on topics such as Web services, ASP.NET, ADO.NET, and Visual Studio .NET. Automatically subscribe today!

About

Tony Patton has worn many hats over his 15+ years in the IT industry while witnessing many technologies come and go. He currently focuses on .NET and Web Development while trying to grasp the many facets of supporting such technologies in a productio...

6 comments
greatbuddyz
greatbuddyz

I have been searching for this quality blogs regarding this niche mensagens para orkut Searching in Yahoo drove me here, I just found this kind of satisfactory readings i was looking for. I must bookmark this website to avoid missing it again. iPhone jailbreak

hushcat
hushcat

I am happy when reading your blog with updated information! thanks alot and hope that you will post more site that are related to this site.Plaster Coving

knroc
knroc

protected void Page_Load(object sender, EventArgs e) { WalkTree(SiteMap.RootNode); } private void WalkTree(SiteMapNode pNode) { // Do this one if ((pNode.Title.Length > 0) && (pNode.Url.Length > 0)) { Response.Write(pNode.Title.ToString() + " -- " + pNode.Url.ToString() + ""); } // Now do its children foreach (SiteMapNode child in pNode.ChildNodes) { WalkTree(child); } }

ralphjb
ralphjb

I am using Web Expression and I am stumpted with this web.sitemap stuff. I am attempting to create a hierarchical sitemap, with nested pages and then use the sitemappath feature to have breadcrumbs back to the original home page. This does not seem to be working beyond one step, however. I am not a programmer. Is there some complete and simple guide out there I can reference to get this done? Thanks!

nivedv
nivedv

Good article will wait for the next one! Good job

Justin James
Justin James

I did not touch sitemap until a few weeks ago, when I reviewed Expression Web (I simply had no use for them), but I do like them quite a bit, now that I have gotten my hands on them. J.Ja

Editor's Picks