Having recently installed BlogEngine.NET, I was interested to see the auto-generated sitemap that Mads had created. Basically, he implemented a custom HttpHandler that when requested would respone with XML that conformed to the Sitemap Protocol 0.9 which is dictated by sitemaps.org. This is an XML schema that all of the major search engines have signed up to which include Google, Yahoo! and Microsoft.
What I wanted to do is to implement this for my own site, but the custom handlers of Mads was tailored to BlogEngine.NET, so it required a bit of tweaking. Now, I have a Web.sitemap for my website, so in the scheme of wanting to do things from a generic point of view, I decided to implement the HttpHandler to utilise the Web.sitemap file to generate the sitemap response. To do this, first we have to load up the sitemap file…
XmlSiteMapProvider xmlSiteMap = new XmlSiteMapProvider();
NameValueCollection myCollection = new NameValueCollection(1);
myCollection.Add(“siteMapFile”, “Web.sitemap”);
xmlSiteMap.Initialize(“provider”, myCollection);
xmlSiteMap.BuildSiteMap();
Then we have to navigate through the tree structure identifying the nodes that we are interested in…
private static void ProcessNode(XmlWriter writer, SiteMapNode node, string attribute)
{
foreach (SiteMapNode siteMapNode in node.ChildNodes)
{
string actualUrl;
if (siteMapNode.HasChildNodes)
{
ProcessNode(writer, siteMapNode, attribute);
}
actualUrl = siteMapNode[attribute];
if (string.IsNullOrEmpty(actualUrl))
{
actualUrl = siteMapNode.Url;
if (string.IsNullOrEmpty(actualUrl))
{
continue;
}
}
WriteUrl(actualUrl, writer);
}
}
And lastly building up the XML response…
if(Uri.IsWellFormedUriString(actualUrl, UriKind.Relative))
{
FileInfo fileInfo = new FileInfo(HostingEnvironment.MapPath(actualUrl));
writer.WriteStartElement(“url”);
writer.WriteElementString(“loc”, HttpContext.Current.Request.Url.AbsoluteUri.Replace(HttpContext.Current.Request.Url.AbsolutePath, “”) + actualUrl);
writer.WriteElementString(“lastmod”, fileInfo.LastWriteTime.ToString(“yyyy-MM-dd”));
writer.WriteElementString(“changefreq”, “monthly”);
writer.WriteEndElement();
}
I have implemented this HttpHandler in a seperate component, simply to allow me to reference this utility from other websites that I have written, but you may decide to simply plug in the HttpHandler to your main project….the decision is yours.
Now, hopefully you’ve also realised that you’ll need to reference the HttpHandler in the website’s web.config……like so…
<httpHandlers>
<add verb=”*” path=”sitemap.axd” type=”CodeConsults.HttpHandlers.Generic.Sitemap” validate=”false”/>
</httpHandlers>
Now you can run your website, point your favoured browser to http://website/sitemap.axd and voila, your sitemap is available for all to see. But the last stage should be to update your robots.txt file to tell the search engines that you have a nice sitemap for them to use. To do this simply open your robots.txt file and enter the following…
sitemap: http://www.codeconsults.com/sitemap.axd