I wanted some easy way to add modules to a website of mine, and have it automatically generate the menu, so I thought I'd compile some existing ideas and code I found into something useful for that end.
In short, what I wanted to do is have a Sitemap generated based on the folders in the application.
First, armed with what I learned about HttpHandlers, I created one to do just that: search folders and .aspx files and create the sitemap as needed by the XmlSiteMapProvider. It hooks to sitemap.axd and outputs the xml.
You can download that code at the end of this article, just drop it in the App_Code folder.
The target format is this
<siteMap>
<siteMapNode title="RootNode" description="This is the root node of the site map. There can be only one root node." url="Page1.aspx" >
<siteMapNode title="ChildofRootNode" description="Descriptions do not have to be unique." url="Page2.aspx">
<siteMapNode title="ChildOfChildNode" description="SiteMapNode objects can be nested to any level." url="Page3.aspx"/>
</siteMapNode>
<siteMapNode title="ChildofRootNode" description="Descriptions do not have to be unique." url="Page4.aspx"/>
</siteMapNode>
</siteMap>
The first problem I run into was that the .NET provider didn't like two things, the extension (.axd) and the fact that the file doesn't really exists, as can be seen quickly using Reflector.

(...)
After thinking for a bit, I decided to implement my own SiteMapProvider, luckily one already existed.
I used Stefan Sedich's code, removed the dynamic node part, and modified the way it looks for the file (which was to map the physical path, but as I said earlier, the file doesn't exist) like this:
siteMapXML = XDocument.Load(GetUrlPath(HttpContext.Current.Request.Url.AbsoluteUri) + "/~/" +this.siteMapFile);
This way, the HttpHandler takes the request, and outputs the sitemap xml.
The result is that for this sample web site:
This xml is generated:
There's also configuration entry in the web.config file to exclude specific folders in the search, nothing too fancy:
<add key="SiteMap_ExcludeFolders" value="App_Code;App_Themes;Bin;MasterPages"/>
So with this, whenever I add a subfolder in my application, it will automatically appear in the Menu, SiteMapPath, or whatever control that uses the sitemap.
I hope this is useful to someone
Downloads
SiteMap.cs (4.28 kb)