Navigation mechanism in ASP.NET 2.0
The coming ASP.NET 2.0 version will ship with builds in mechanism of navigation between web application pages. This mechanism enables to define hierarchic relations between pages and to use dedicated components to show the current user location in the pages hierarchy and the overall hierarchy tree, in tree view style. By default ASP.NET use XML file “app.sitemap” to define page relations and to use those definitions in runtime. However you can implement ISiteMapProvider to create your own data source (such as Database). SiteMap is the object that holds in memory the pre-define definitions. There are two components that based on SiteMap :
1) SiteMapPath that display the current page position in the hierarchy. By default SiteMapPath show the pages as links which enable the user to jump back to page that he already visit (Base->Orders->My Orders).
2) SiteMapDataSource that serve as data source for other controls such as Treeview to show the pages "tree".
The XML file that holds the pages definitions is build from two tags: SiteMap and SiteMapNode . SiteMap holds all the pages declarations. SiteMapNode stand for Page and he can contain recursively others SiteMapNodes that will display and act as the father SiteMapNode pages. A simple app.sitemap might look like this :
<?xml version="1.0" encoding="utf-8" ?>
<siteMap>
<siteMapNode title="Home" url="default.aspx">
<siteMapNode title="Articles" url="articles/articles.aspx">
<siteMapNode title="Article 1" url="articles/demoarticle1.aspx" />
<siteMapNode title="Article 2" url="articles/demoarticle2.aspx" />
<siteMapNode title="Article 3" url="articles/demoarticle3.aspx" />
</siteMapNode>
<siteMapNode title="Picture Gallery" url="PhotoAlbum/PhotoAlbums.aspx">
<siteMapNode title="Meetings" url="PhotoAlbum/PictureAlbum.aspx?albumid=1" />
<siteMapNode title="Activities" url="PhotoAlbum/PictureAlbum.aspx?albumid=2" />
<siteMapNode title="Training" url="PhotoAlbum/PictureAlbum.aspx?albumid=3" />
</siteMapNode>
</siteMapNode>
</siteMap>
SiteMap is the object that holds the pages definition in memory and you can use its property and methods to navigate through those definitions and enforce navigation rules on your site. Below you can find a simple example of enforcing navigation by the hierarchy. This is not full working example, it’s just for demonstration purpose. If your site use master pages when the master page is load a check is perform to validate that the referrer page is the same page that declared as the current page father in XML definitions. If the validation will fail an Error HTML will be return to the client (but you can transfer the user to the right page). This code will prevent direct navigation from the default page to one of the articles. To access one of the articles the user must visit the articles page first.
void Page_Load(object sender, System.EventArgs e(
{
if (this.Request.UrlReferrer != null(
}
if (this.Request.UrlReferrer.AbsolutePath != SiteMap.CurrentNode.ParentNode.Url (
}
Response.Write )"illegal operation");
Response.End();
{
{
}
I found the Navigation mechanism easy to use and customize. It really save some code writing and can be use to other tasks.
