Reputation: 2295
I am trying to auto generate the sitemap, I am using the following Web.config:
<appSettings>
<add key="MvcSiteMapProvider_IncludeAssembliesForScan" value="WebUI" />
<add key="MvcSiteMapProvider_ScanAssembliesForSiteMapNodes" value="true" />
<add key="MvcSiteMapProvider_UseExternalDIContainer" value="false" />
<add key="MvcSiteMapProvider_UseExternalDIContainer" value="false"/>
<add key="MvcSiteMapProvider_SiteMapFileName" value="~/Mvc.sitemap"/>
<add key="MvcSiteMapProvider_ExcludeAssembliesForScan" value=""/>
<add key="MvcSiteMapProvider_IncludeAssembliesForScan" value=""/>
<add key="MvcSiteMapProvider_AttributesToIgnore" value=""/>
<add key="MvcSiteMapProvider_CacheDuration" value="5"/>
<add key="MvcSiteMapProvider_ControllerTypeResolverAreaNamespacesToIgnore" value=""/>
<add key="MvcSiteMapProvider_DefaultSiteMapNodeVisibiltyProvider" value=""/>
<add key="MvcSiteMapProvider_VisibilityAffectsDescendants" value="true"/>
<add key="MvcSiteMapProvider_SecurityTrimmingEnabled" value="false"/>
<add key="MvcSiteMapProvider_EnableLocalization" value="true"/>
<add key="MvcSiteMapProvider_EnableSitemapsXml" value="true"/>
<add key="MvcSiteMapProvider_EnableResolvedUrlCaching" value="true"/>
<add key="MvcSiteMapProvider_EnableSiteMapFile" value="true"/>
<add key="MvcSiteMapProvider_IncludeRootNodeFromSiteMapFile" value="true"/>
<add key="MvcSiteMapProvider_EnableSiteMapFileNestedDynamicNodeRecursion" value="false"/>
<add key="MvcSiteMapProvider_UseTitleIfDescriptionNotProvided" value="true"/>
</appSettings>
Controller:
[MvcSiteMapNode(Title = "Healthcare", Key = "Healthcare", ParentKey = "Home")]
public ActionResult Healthcare (){..}
The sitemap MVC.sitemap does not update? I changed the following line to ture
<add key="MvcSiteMapProvider_ScanAssembliesForSiteMapNodes" value="true"/>
Got this error:
Not all configured nodes could be paired with a parent node. Check your parent keys to ensure that a node with a corresponding key exists in the SiteMap. Note that the match is case sensitive.
SiteMapCacheKey: 'sitemap://localhost/'
Orphaned Nodes:
ParentKey: 'Home' | Controller: 'Home' | Action: 'Healthcare' | Area: '' | URL: '/Home/Healthcare' | Key: 'Healthcare' | Source: 'MvcSiteMapNodeAttribute'
Would appreciate your suggestions.
Upvotes: 1
Views: 5237
Reputation: 56859
There is currently no built-in way to automatically generate the SiteMap. Each node must be manually configured using one of the available node definition options (XML file, [MvcSiteMapNode]
attribute, dynamic node providers, or ISiteMapNodeProvider).
The primary reason for this is that MVC has no pre-defined hierarchy of pages, only controllers and actions, so you would end up with a SiteMap object that has a root node with all of the pages just below it. It would also not be possible to auto detect all of the possible values for action parameters. A scanned SiteMap like that would be useless for site navigation in most cases.
That said, if you only want to create an XML sitemap to submit to search engines (which is flat anyway), or you can think of a way to tell MvcSiteMapProvider how to nest the key/parent key relationships for navigation, you could implement ISiteMapNodeProvider and scan for controllers and actions yourself (you could use the ReflectionSiteMapNodeProvider as a guide).
Some Pointers for Your Existing Configuration:
Setting the MvcSiteMapProvider_ScanAssembliesForSiteMapNodes
setting to true is required when using [MvcSiteMapNode]
attribute, so that part you got right.
However, you have the MvcSiteMapProvider_IncludeAssembliesForScan
and MvcSiteMapProvider_UseExternalDIContainer
settings defined twice.
The error indicates there is no node in your SiteMap that has a key named "Home", so it doesn't know where in the hierarchy to put the [MvcSiteMapNode]
you have configured.
Since you have set both MvcSiteMapProvider_EnableSiteMapFile
and MvcSiteMapProvider_IncludeRootNodeFromSiteMapFile
to true, you must define your root node in the Mvc.sitemap file, and give it an explicit key "Home" (if that is what you intended, anyway).
<mvcSiteMapNode title="Home" controller="Home" action="Index" key="Home">
<!-- If you have any additional nodes you want to define in XML, put them here -->
</mvcSiteMapNode>
If you only want to use [MvcSiteMapNode]
for configuring your nodes (with no XML configuration), you should set the MvcSiteMapProvider_EnableSiteMapFile
setting to false and delete the Mvc.sitemap and MvcSiteMapProvider.xsd files. In this case, you will need to define a root node (a node with no parent key) using [MvcSiteMapNode]
.
[MvcSiteMapNode(Title = "Home", Key = "Home")]
public ActionResult Index()
{
return View();
}
Upvotes: 1