sitemap with attributes in code only?

Nov 1, 2010 at 10:20 PM


I want to create a sitemap only with attributes in code. If I don't create an empty sitemap xml file, I get an exception in DefaultSiteMapProvider.Initialize(). The strange thing is that an exception is raised in this method if the file does not exist but there is no such exception in the BuildSiteMap method later if I comment the first exception. On the other side, if I create an empty xml file, it will crash because it can't find any root node inside (my root node is defined via an attribute in the code). So I can make it work if I comment the exception raising. However, the generated sitemap is totally lacking titles and description:


My attribute is defined like this for the root node:

[MvcSiteMapNodeAttribute(Title = "Homepage", Key = "Root", Description = "my description" )]

Thanks for your help.

Nov 2, 2010 at 2:38 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Nov 17, 2010 at 7:26 PM

I see that you changed the code to avoid the exceptions. But even then, the generated sitemap is as I showed above: it has no title and description even if my actionfilter attribute sets them.

Nov 18, 2010 at 6:54 AM

Would it be possible to post some code of your SiteMapNodeAttributes ?

Nov 18, 2010 at 6:55 AM

Nevermind: XML sitemap format does not allow for a title attribute, so this is normal behaviour.

Nov 22, 2010 at 4:38 PM
Edited Nov 22, 2010 at 4:47 PM

ok I understand.

However there is still an issue. If I want a dynamic sitemap for the blog of my site, I must create an xml sitemap containing only:

<?xml version="1.0" encoding="utf-8" ?>
<mvcSiteMap xmlns="" enableLocalization="true">
<mvcSiteMapNode title="Details" action="Details" dynamicNodeProvider="Namespace.Web.NewsArticlesDynamicNodeProvider, Namespace.Web" />

It contains no other nodes since they are all in my code through attributes. In that case, I get an exception when visiting sitemap.xml:
Value cannot be null.
Parameter name: rawUrl

[ArgumentNullException: Value cannot be null.
Parameter name: rawUrl]
   System.Web.StaticSiteMapProvider.FindSiteMapNode(String rawUrl) +2620163
   MvcSiteMapProvider.DefaultSiteMapProvider.AddNode(SiteMapNode node) +98
   MvcSiteMapProvider.DefaultSiteMapProvider.SetRootNode(SiteMapNode rootNode) +31
   MvcSiteMapProvider.DefaultSiteMapProvider.BuildSiteMap() +303

[MvcSiteMapException: An error occured while building the sitemap... Check the InnerException for more details.]
   MvcSiteMapProvider.DefaultSiteMapProvider.BuildSiteMap() +1031
   MvcSiteMapProvider.DefaultSiteMapProvider.GetRootNodeCore() +54
   MvcSiteMapProvider.DefaultSiteMapProvider.get_RootNode() +10
   System.Web.SiteMap.get_RootNode() +45
   MvcSiteMapProvider.Web.XmlSiteMapController.Index(Int32 page) +23

The second time I visit the page, the exception has gone and I get only one root node for the homepage. It's as if specifying a siteMapFile entry in web.config will prevent attributes
to be searched.

Also what means "action" in
Nov 22, 2010 at 5:01 PM
Edited Nov 22, 2010 at 5:14 PM

I found out that I was able to avoid the xml file and have this attribute instead:

[MvcSiteMapNodeAttribute(ParentKey = "News", DynamicNodeProvider = "Namespace.Web.NewsArticlesDynamicNodeProvider, Namespace.Web")]

In that case, my provider is called but I now get 2 issues:

1. If I set a sliding expiration of 1 second so that it's called at every request, it's still never called again.

2. If I set the parent key of a dynamic node to an existing key set by an attribute, all the dynamic nodes are set at the root level and not under the parent key.