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.

 

 

5 Comments

  • Can you fix the huge whitespace after the end of your entry? It's kind of annoying...

  • Hmmm...

    All of this automation that Microsoft added to ASP.NET... is this really a good thing? It seems like there's not that much left to do, for us web developers, since MS has done it all for us... :/

  • How static/dynamic is this sitemap thing really?

    We at DNV have a database containing 5000 ships. Each ship have from 100-2000 web pages each with information about them. (I think this is quite the same as somebody with a large product database).

    The data (and thus the exact pages) is constantly changing, at least a bit. For instance, one boat get a particular pump installed. This creates one or more new pages with the information about this particular pump on this particular ship (type, tests, drawings, etc)



    OK, so here is the question:

    Is such a system where the amount of pages are very large, and constantly changing, can the sitemap system be used?

    Seems to me that every part of the system must be provided &quot;on demand&quot;, there and then, for it to be usefull.



    tor

  • Tor Hi,



    After I finish such a project using ASP.NET 2.0 I can answer your questions. Theoretically yes.

  • Can you please email me the code for this script or please let know me in detail in relation to this script?

Comments have been disabled for this content.