2.4. Create a Site Map for Your Web Site
Note: Your users will never be lost again if you create a site map for your web site!
Unless your web site contains only a single web page, you need a way for users to easily navigate between the pages of your site. In ASP.NET 2.0, there are four new controls that make it easy to implement site navigation. These controls are:
In the remaining labs in this chapter, you will learn how to use these controls to make it easy for users to navigate your web site. But first, let's take a closer look at the SiteMapDataSource and SiteMapPath controls.
2.4.1. How do I do that?
In this lab, you'll see how you can use the SiteMapPath control together with the Web.sitemap file to display navigation paths on your site pages. First, you'll create a web application that uses a Master page. When a Master page is used as a template for all other pages in a site, it's a good place to put a site map and controls that help users find their way around, because that information will be visible on every page a user visits. You'll create a site map file that describes the logical relationship between the pages of your site, and then you'll add a SiteMapPath control to the Master page so users can see where they are in the hierarchy and move "up" and "down" the path to their current page.
2.4.2. How it works
The SiteMapPath control by default reads the content of the Web.sitemap file and displays the site navigation path leading to the currently displayed page.
Tip: It is important to note that the SiteMapPath control displays its path information based on whatever is in the Web.Sitemap file. It does not necessarily mean that it will correspond to the physical organization of the site. If the content of the Web.sitemap file does not reflect your actual site structure, the information displayed by the SiteMapPath control would be incorrect as well.
Note that in ASP.NET 2.0 there is only one possible site map provider: XmlSiteMapProvider. The XmlSiteMapProvider reads site map information from a predefined XML document and, by default, that is the Web.sitemap file.
If you need to change the default name of the site map file, you can do so via the Web.config file. Example 2-3 shows how you can deregister the default XmlSiteMapProvider and register it again with a different site map name: My.sitemap (you can also register a new site map provider, such as one you've written yourself).
Example 2-3. Changing the default site map in Web.config
<configuration> <system.web> <siteMap> <providers> <remove name="AspNetXmlSiteMapProvider" /> <add name="AspNetXmlSiteMapProvider" type="System.Web.XmlSiteMapProvider, System.Web, Version=1.2.3400.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" siteMapFile="My.sitemap" /> </providers> </siteMap> </system.web> </configuration>
Tip: If you have created your own SiteMapProvider, change the SiteMapProvider property of the SiteMapPath control to the name of your own SiteMapProvider.
Note: Chapter 5 will discuss the provider model that is new in ASP.NET 2.0 in greater detail.
2.4.3. What about...
...customizing the SiteMapPath control?
The default view of the SiteMapPath control displays the navigation path using the string contained in the title attribute of the <siteMapNode> element in the site map file. Each navigation path is separated by the ">" character.
You can customize the look and feel of the SiteMapPath control by adding style information to it. You can customize the SiteMapPath control by switching to Source View of the page that contains the control (in our example it is the Master page).
Example 2-4 shows how you can override the default path-separator character and replace it with an Image control (the image used is saved in the Images folder of the application as arrow.gif ). The code also highlights the current path using a light green background color.
Example 2-4. Customizing the SiteMapPath control
<asp:SiteMapPath Font-Name="Garamond" Font-Size="12pt" RunAt="server"> <CurrentNodeStyle Height="24px" BackColor="LightGreen" Font-Bold="true" /> <NodeStyle Height="24px" /> <PathSeparatorTemplate> <ItemTemplate> <asp:Image ImageUrl="~/Images/arrow.gif" RunAt="server" /> </ItemTemplate> </PathSeparatorTemplate> </asp:SiteMapPath>
Figure 2-23 shows how the SiteMapPath control looks like after the modification.
Figure 2-23. Customizing the look and feel of the SiteMapPath control
You can also programmatically modify the properties of the SiteMapPath control through the following properties:
...accessing site map information programmatically?
If you are not using a SiteMapPath control, you can still programmatically access site map information through the Site Map API (found in the System.Web.SiteMap namespace). Example 2-5 (located in the code-behind of a Master page) shows how to display site navigation information programmatically.
Example 2-5. Displaying site information programmatically
Partial Class MasterPage_master Sub Page_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) _ Handles Me.Load Dim myCurrentNode As SiteMapNode '---Gets the current node myCurrentNode = SiteMap.CurrentNode '---displays the navigational path--- Dim path As String = myCurrentNode.Title While myCurrentNode.ParentNode IsNot Nothing myCurrentNode = myCurrentNode.ParentNode path = " > " & path path = myCurrentNode.Title & path End While Response.Write(path) End Sub End Class
Besides the CurrentNode property, additional properties are available for accessing site information, including:
...hiding nodes from users who lack proper authorization?
If you examine the Web.sitemap file carefully, you will notice the role attribute. In the examples thus far, the role attribute is set to an empty string. By default, the SiteMapDataSource control makes all the nodes information in Web.sitemap visible. However, there are times when you would like to restrict the display of certain nodes to a particular group of users. For example, normal users should not be able to see the path to the admin web page, and so the SiteMapDataSource control should allow nodes to be hidden (or trimmed). This feature is known as security trimming. The XmlSiteMapProvider disables security trimming by default; to enable security trimming, you need to perform the following steps:
Example 2-6. Web.config for the Administrator directory
<configuration xmlns= "http://schemas.microsoft.com/.NetConfiguration/v2.0"> <system.web> <compilation debug="false"/> <!-- deny all users --> <authorization> <deny users="*" /> </authorization> </system.web> <!-- allow only Admin role users to see Admin.aspx --> <location path="Admin.aspx"> <system.web> <authorization> <allow roles="Admin" /> </authorization> </system.web> </location> </configuration>
Once you have made these changes, only users in the admin role will be able to see the Administrator node.
Note: See Chapter 5 for more details on roles and membership.
2.4.4. Where can I learn more?
For more information on ASP.NET authorization, check out the following MSDN Help topic: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconaspnetauthorization.asp.
To learn more about Site Map Providers, check out the MSDN Help topic "ASP.NET Site Navigation Providers."
If the included AspNetXMLSiteMapProvider does not suit your purpose and you want to store your site map information in a data store other than XML (such as an Oracle database), check out the MSDN Help topic "Implementing a Site Map Provider."