If you've ever traveled to a strange destination, you know the importance of maps-they can help make your trip more comfortable. This is the same reason for the site. A website should be presented to visitors with a simple and flexible navigation structure so that they can easily navigate to different parts of the site. ASP.net 2.0 provides a feature called a Sitemap-it helps you achieve this functionality. This article explains what a site map is and describes how to develop a site navigation structure that uses them.
First, SiteMap
A site map is an XML file (with a. sitemap extension)-it can describe the entire navigation layout of your site in detail. You can use the sitemap file to meet all your requirements.
An example will help explain the site-map file. Figure 1 shows the directory structure of a sample web site.
Figure 1: Site structure
In the diagram, the home (Default.aspx) and contact us pages (contact.aspx) are located under the root folder of the Web site. There are also two subfolders called Products and services, respectively. There are two Web Forms under each subfolder: corresponding to Product1.aspx and product2.aspx, and service1.aspx and service2.aspx respectively.
Now, if you follow the steps below, you can use a site map to describe the structure of the site:
1. Create a new Web site using Vs.net 2005.
2. Right click on the website and select "Add New Item ...".
3. From the "Add New Item ..." dialog box, select the site map (see Figure 2) and name it Web.sitemap.
Figure 2: Adding a new site map
The Key in the following XML markup in the Web.sitemap file:
<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap- File-1.0" >
<siteMapNode url="default.aspx" title="Home" description="My Web Site">
<siteMapNode url="~/products/default.aspx" title="Products">
<siteMapNode url="~/products/product1.aspx" title="First Product" />
<siteMapNode url="~/products/product2.aspx" title="Second Product" />
</siteMapNode>
<siteMapNode url="~/services/default.aspx" title="Services">
<siteMapNode url="~/services/service1.aspx" title="First Service" />
<siteMapNode url="~/services/service2.aspx" title="Second Service" />
</siteMapNode>
<siteMapNode url="contact.aspx" title="Contact Us" />
</siteMapNode>
</siteMap>
The root of the site map file is the sitemap. It contains a node SiteMapNode and, depending on your site structure, it can contain a number of SiteMapNode nodes.
This SiteMapNode tag has four important attributes (see table 1).
Property |
Describe |
Title |
Displays the title of the page. This property is often used by navigation controls to display the title of the URL. |
Url |
Displays the URL of the page described by this node. |
Description |
Specifies a description of this page. You can use this description to display the content of the hint. |
Roles |
By using a security overhaul (discussed later), this property specifies the role that is allowed to access this page. |
Key attributes for Table 1:<sitemapnode> tags
This makes up your site map. Now you can use it for navigation purposes.