DNAideXmlSiteMapProvider

A number of key extensions to the default XmlSiteMapProvider:
  • Simple "hook in" to the DNAide UrlRewriter
  • You can now set the value of the "key" attrbute (not available with the XmlSiteMapProvider)
  • Virtual method "BuildSiteMapFile" - a simple way to generate dynamic sitemaps (see "More Advanced Example" below).

Simple Configuration

<system.web>
	<siteMap enabled="true" defaultProvider="DNAideXmlSiteMapProvider">
		<providers>
			<clear/>
			<add name="DNAideXmlSiteMapProvider" type="DNAide.Web.DNAideXmlSiteMapProvider" siteMapFile="Web.sitemap" />
		</providers>
	</siteMap>
</system.web>


Simple SiteMap Example

<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
	<siteMapNode key="home" url="~/index.htm" title="Home" rewriteToUrl="~/_Templates/WebForms/Home.aspx">	
		<siteMapNode key="news-list" url="~/news/index.htm" title="Latest News" rewriteToUrl="~/_Templates/WebForms/NewsList.aspx">
			<siteMapNode key="news-1" url="~/news/dnaide-released.htm" rewriteToUrl="~/_Templates/WebForms/NewsView.aspx" />
			<siteMapNode key="news-2" url="~/news/another-story.htm" rewriteToUrl="~/_Templates/WebForms/NewsView.aspx" />
			<siteMapNode key="news-3" url="~/news/more-news.htm" rewriteToUrl="~/_Templates/WebForms/NewsView.aspx" />
			<siteMapNode key="news-4" url="~/news/oldest-news.htm" rewriteToUrl="~/_Templates/WebForms/NewsView.aspx" />
		</siteMapNode>
	</siteMapNode>
</siteMap>


Some interesting points about the above example:
  • Shows that you can set the "key" attribute
  • Additional (optional) attribute "rewriteToUrl". Best example being the individual news stories. Each story rewrites to the same ASPX template which will render the correct story. In the above example, I have stored the id of each story as part of the "key". The "NewsView.aspx" template can retrieve the current key value to determine which story to load from the database
  • Used ".htm" rather than ".aspx" for urls. This is entirely optional but to enable this, ensure that IIS is configured so that the .htm extension is mapped to ASP.NET

Rewrite attributes (requires using the DNAide UrlRewriter)

The following are optional:
  • rewriteToUrl: See above
  • redirectToSiteMapNodeKey: Redirect to another node in the sitemap
  • redirectStatusCode: i.e. what is added to the response header e.g. 301, 404 etc. Default = "301"

More Advanced Example

In the simple example above, the template that rendered news stories "NewViews.aspx" retrieves stories from a database. However, the siteMapNodes are not from a database but "hand written" into the "Web.sitemap".

The DNAideXmlSiteMapProvider class exposes a virtual "BuildSiteMapFile" method which is fired by the provider if the associated website file cannot be found. So a solution to the news stories:
  • Create a NewsXmlSiteMapProvider class inheriting from DNAideXmlSiteMapProvider and override the "BuildSiteMapFile" to read stories from the database and generate a relevant sitemap file.

public class NewsXmlSiteMapProvider : DNAideXmlSiteMapProvider
{
	public override void BuildSiteMapFile()
	{
		XmlWriterSettings settings = new XmlWriterSettings();
		settings.Indent = true;
		settings.IndentChars = "\t";
		settings.ConformanceLevel = ConformanceLevel.Document;

		using (XmlWriter writer = XmlWriter.Create(SiteMapPath, settings))
		{
			writer.WriteStartElement("siteMap", "http://schemas.microsoft.com/AspNet/SiteMap-File-1.0");
			writer.WriteStartElement("siteMapNode");
			writer.WriteAttributeString("key", "news-list");
			writer.WriteAttributeString("url", "~/news/index.htm");
			writer.WriteAttributeString("title", "Latest News");
			writer.WriteAttributeString("rewriteToUrl", "~/_Templates/WebForms/NewsList.aspx");
			
			// Retrieve news stories from the database (or whereever) and write out SiteMapNodes

			writer.WriteEndElement(); // Close news-list
			writer.WriteEndElement(); // Close siteMap
			writer.Flush();
		}
	}
}

  • Add the "NewsXmlSiteMapProvider" into your web.config

<siteMap enabled="true" defaultProvider="Main">
	<providers>
		<clear/>
		<add name="Main" type="DNAide.Web.DNAideXmlSiteMapProvider" siteMapFile="Web.sitemap"/>
		<add name="News" type="NewsXmlSiteMapProvider" siteMapFile="News.sitemap"/>
	</providers>
</siteMap>

  • Create a SiteMapNode in "Web.sitemap" using the "NewsXmlSiteMapProvider".

<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
	<siteMapNode key="home" url="~/index.htm" title="Home" rewriteToUrl="~/_Templates/WebForms/Home.aspx">	
		<siteMapNode provider="News" />
	</siteMapNode>
</siteMap>



Using the above example is a simple way to generate dynamic sitemaps. You have full control of the source for you sitemap i.e. you are not tied to SQLServer etc.

In the above examples, to get the sitemap to rebuild the sitemap file, simply remove it from your website e.g. if an administrator adds / updates / or removes a news story, delete the News.sitemap file and the file will then be recreated.

Last edited Mar 10, 2007 at 11:53 AM by stebrennan, version 2

Comments

No comments yet.