![]() Figure 1: Website structure |
![]() Figure 2: Adding a new sitemap |
<?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> |
Property | description |
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 about this page. You can use this description to display prompt content. |
roles | By using security trimming (discussed later), this attribute specifies the roles that are allowed to access this page. |
![]() Figure 3: Breadcrumb navigation |
Web form name | folder |
Default.aspx | Website root |
Contact.aspx | Website root |
Default.aspx | Products |
Product1.aspx | Products |
Product2.aspx | Products |
Default.aspx | Services |
Service1.aspx | Services |
Service2.aspx | Services |
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Label ID="Label1" runat="server" Font-Size="XX-Large" ForeColor="Blue" Text="Welcome!"></asp:Label><br /> <asp:SiteMapPath ID="SiteMapPath1" runat="server" Font-Names="Verdana" Font-Size="0.8em" PathSeparator=" : "> <PathSeparatorStyle Font-Bold="True" ForeColor="#5D7B9D" /> <CurrentNodeStyle ForeColor="#333333" /> <NodeStyle Font-Bold="True" ForeColor="#7C6F57" /> <RootNodeStyle Font-Bold="True" ForeColor="#5D7B9D" /> </asp:SiteMapPath> <br /> <br /> <asp:contentplaceholder id="ContentPlaceHolder1" runat="server"> </asp:contentplaceholder> </div> </form> </body> </html> |
![]() Figure 4: Sample run of Default.aspx |
HyperLink ID | Text property | NavigateUrl property |
HyperLink1 | Products | ~/products/default.aspx |
HyperLink2 | Services | ~/Services/default.aspx |
HyperLink3 | Contact Us | ~/contact.aspxTable |
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Title="Untitled Page" %> <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> <center> <table> <tr> <td width="60%"> <asp:Label ID="Label1" runat="server" Font-Size="X-Large" Text="Welcome to our Web site!"></asp:Label></td> </tr> <tr> <td width="60%"> <asp:HyperLink ID="HyperLink1" runat="server" Font-Size="X-Large" NavigateUrl="~/Products/Default.aspx">Products </asp:HyperLink></td> </tr> <tr> <td width="60%"> <asp:HyperLink ID="HyperLink2" runat="server" Font-Size="X-Large" NavigateUrl="~/Services/Default.aspx">Services </asp:HyperLink></td> </tr> <tr> <td width="60%"> <asp:HyperLink ID="HyperLink3" runat="server" Font-Size="X-Large" NavigateUrl="~/Contact.aspx">Contact Us </asp:HyperLink></td> </tr> </table> </center> </asp:Content> |
![]() Figure 5: Default page of the Products folder |
HyperLink ID | Text property | NavigateUrl property |
HyperLink1 | First Product | ~/products/product1.aspx |
HyperLink2 | Second Product | ~/products/product2.aspx |
![]() Figure 6. Default page of Services folder |
HyperLink ID | Text property | NavigateUrl property |
HyperLink1 | First Service | ~/Services/service1.aspx |
HyperLink2 | Second Service | ~/Services/service2.aspx |
Text attribute of | Web form name | Label
~/Contact.aspx | Contact Us |
~/Products/Product1.aspx | First Product Details |
~/Products/Product2.aspx | Second Product Details |
~/Services/Service1.aspx | First Service Details |
~/Services/Service2. aspx | Second Service Details |
![]() Figure 7: Sample run of Product1.aspx |
Notice how the title and URL attributes of the web.sitemap file are used to generate "breadcrumbs". Also, notice how the parent is displayed along with the current page title. Try navigating to various pages and observing the SiteMapPath control.
4. Use SiteMap data source control
The use of site maps is not limited to the SiteMapPath control. You can also attach a sitemap to a navigable control (such as a TreeView). In the following example, you will use the same site map file to implement binding to a TreeView control.
Add a new Web form SiteMapDataSourceDemo.aspx to the website. Then, drag a SiteMap data source control (SiteMapDataSource1) and a TreeView control (TreeView1) onto the form. Set the DataSourceID property of this TreeView control to SiteMapDataSource1. In addition, set the ShowLines property of the TreeView control to true. Here is the complete markup from the SiteMapDataSourceDemo.aspx page:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SiteMapDataSourceDemo.aspx.cs" Inherits="SiteMapDataSourceDemo" %> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <asp:TreeView ID="TreeView1" runat="server" DataSourceID="SiteMapDataSource1" ShowLines="True"> </asp:TreeView> <asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" /> </form> </body> </html> |
Now, run the Web form to see how the same navigation structure is automatically generated into the TreeView (see Figure 8).
![]() Figure 8: Binding the sitemap file to a TreeView control |
Property | description |
ChildNodes | represents the collection of all child nodes of the current node |
HasChildNodes | indicates whether the site map node has child nodes (true/false) |
Title | returns the value of the title attribute specified in the site map file |
Url | returns in the site map file The value of the specified url attribute |
Description | returns the value of the description attribute specified in the site map file |
ParentNode | indicates the reference of the parent site map node of the current node |
protected void Page_Load(object sender, EventArgs e) { int count = SiteMap.RootNode.ChildNodes.Count; for (int i = 0; i < count; i++) { SiteMapNode smNode=SiteMap.RootNode.ChildNodes[i]; TreeNode tvNode = new TreeNode(smNode.Title, "", "", smNode.Url, ""); TreeView1.Nodes.Add(tvNode); if (smNode.HasChildNodes) { int childCount=smNode.ChildNodes.Count; for (int j = 0; j < childCount; j++) { SiteMapNode smChildNode = smNode.ChildNodes[j]; TreeNode tvChildNode = new TreeNode(smChildNode.Title, "", "", smChildNode.Url, ""); tvNode.ChildNodes.Add(tvChildNode); } } } } |
Here, you first get the total number of child nodes in the root node. Then, you loop through the ChildNodes collection of the root node. On each iteration, you create a new instance of the TreeNode class and specify its title and url in its constructor. Then, you add this TreeNode to the TreeView's Nodes collection. Then, you check if the current SiteMapNode has any child nodes. If there is, you traverse it and repeat the TreeNode creation process. Note that this time you add the new TreeNodes to the current TreeNode object's ChildNodes collection.
Note that you use 2 in the loop because you know there are only two levels of nesting. To make your logic more general, you can use recursion to populate the TreeView.
Run the web form and you'll see something similar to Figure 8 again.
6. Use safety maintenance
Often, websites implement a role-based security model. For example, you can have different roles in your application such as system administrator, product tester, and service tester. In situations like this, you often need to control the site navigation links that are displayed to users. For example, if the currently logged in user has the product tester role, you might want to display only links related to the product and hide any other links. One way to handle roles is to use hand coding, however, it requires implementing all authorization logic programmatically. Fortunately, sitemap files and the SiteMap data source control together provide a feature called security trimming to help you.
In order to test the security overhaul, you need to enable your site's membership and role features. Open the web.config file and add the following tags:
<authentication mode="Forms" /> <authorization> <deny users="?"></deny> </authorization> |
<roleManager enabled="true" /> |
![]() Figure 9: Adding roles using Web site management tools |
![]() Figure 10: Creating users using web site management tools |
<?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 title="Products" roles="ProductTesters"> <siteMapNode url="~/products/product1.aspx" title="First Product" /> <siteMapNode url="~/products/product2.aspx" title="Second Product" /> </siteMapNode> <siteMapNode title="Services" roles="ServiceTesters"> <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> |
<siteMap defaultProvider="myprovider" enabled="true"> <providers> <add name="myprovider" type="System.Web.XmlSiteMapProvider " siteMapFile="SecurityTrimming.sitemap" securityTrimmingEnabled="true" /> </providers> |
![]() Figure 11: Login page |
![]() Figure 12: Use safety refurbishment |