Creating a Custom Site Map Provider


One of the first issues that I encountered when building the e-commerce application was the problem of representing product categories.

On the one hand, I wanted to use all the standard Navigation controls. I wanted to use the Menu, SiteMapPath, and TReeView controls to display the product categories. For example, as you move from page to page in the website, I wanted the SiteMapPath control to display the current product category automatically so that a customer could navigate back up the category hierarchy. Because I wanted to use the product categories with the standard navigation controls, I needed to represent the categories in a Site Map.

Note

Navigation controls and Site Maps are discussed in Part V of this book, "Site Navigation."


On the other hand, I wanted to enable administrators of the application to be able to modify product categories easily through a form interface. I wanted an administrator to be able to add and edit categories through the form interface contained in the Manage\Categories\Default.aspx page.

Unfortunately, the default Site Map provider doesn't support both requirements. The default Site Map provider uses XML files to represents the navigational structure of a website. An administrator cannot easily update an XML file through a form interface. Therefore, I decided to write a custom Site Map provider.

Product categories are represented with the CategorySiteMapProvider. This class is contained in the App_Code folder. The CategorySiteMapProvider stores navigation information in a SQL database table. The CategorySiteMapProvider is configured to retrieve the categories from a database table named Categories.

The ASP.NET Beer Store actually uses both the standard XmlSiteMapProvider and the CategorySiteMapProvider to represent a Site Map. The root Web.sitemap file is contained in Listing 34.1.

Listing 34.1. Web.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="Home page">     <siteMapNode provider="CategorySiteMapProvider" />     <siteMapNode url="~/shoppingcart.aspx" title="Shopping Cart"           description="View Shopping Cart" />     <siteMapNode url="~/contactinfo.aspx" title="Contact Us"           description="Contact us by phone or email" />     <siteMapNode siteMapFile="~/manage/web.sitemap" />   </siteMapNode> </siteMap> 

Notice that the second siteMapNode element in Listing 34.1 specifies a particular provider. This node retrieves all its subnodes from the CategorySiteMapProvider.

Notice, furthermore, that the last siteMapNode element uses the siteMapFile attribute. The Site Map for the management section of the website is contained in a separate XML Site Map file located at \Manage\Web.sitemap.

The ASP.NET Framework is smart enough to merge the SiteMapNodes from these different providers and locations seamlessly in the background. I was surprised with how little work I had to perform to combine the SiteMapNodes from the XML Site Map files and the SQL Server database table.




ASP. NET 2.0 Unleashed
ASP.NET 2.0 Unleashed
ISBN: 0672328232
EAN: 2147483647
Year: 2006
Pages: 276

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net