Using asp.net MVCSiteMapProvider v4 with 2 sitemap

2019-08-25 10:29发布

Hi I would like to use asp.net MVCSiteMapProvider v4 with 2 different sitemaps, one for the main site and one for the admin area. I have done some search and I have found that you need insert it on web.config:

 <siteMap defaultProvider="AppSiteMapProvider" enabled="true">
  <providers>
    <clear />
    <add name="AppSiteMapProvider" type="MvcSiteMapProvider.DefaultSiteMapProvider,
    MvcSiteMapProvider" siteMapFile="~/Mvc2.sitemap"
    securityTrimmingEnabled="true" cacheDuration="5"
    enableLocalization="true" scanAssembliesForSiteMapNodes="true" 
    includeAssembliesForScan="" excludeAssembliesForScan="" 
    attributesToIgnore="visibility"  
    nodeKeyGenerator="MvcSiteMapProvider.DefaultNodeKeyGenerator,
    MvcSiteMapProvider" 
    controllerTypeResolver="MvcSiteMapProvider.DefaultControllerTypeResolver, 
    MvcSiteMapProvider" 
    actionMethodParameterResolver="MvcSiteMapProvider.DefaultActionMethodParameterResolver, 
    MvcSiteMapProvider" aclModule="MvcSiteMapProvider.DefaultAclModule, 
    MvcSiteMapProvider" 
    siteMapNodeUrlResolver="MvcSiteMapProvider.DefaultSiteMapNodeUrlResolver, MvcSiteMapProvider" 
    siteMapNodeVisibilityProvider="MvcSiteMapProvider.DefaultSiteMapNodeVisibilityProvider, MvcSiteMapProvider" 
    siteMapProviderEventHandler="MvcSiteMapProvider.DefaultSiteMapProviderEventHandler, MvcSiteMapProvider" />

    <add name="AdminSiteMapProvider" type="MvcSiteMapProvider.DefaultSiteMapProvider,
    MvcSiteMapProvider" siteMapFile="~/Areas/Admin/Mvc.Sitemap" 
    securityTrimmingEnabled="true" cacheDuration="5" enableLocalization="true" 
    scanAssembliesForSiteMapNodes="true" includeAssembliesForScan="" 
    excludeAssembliesForScan="" attributesToIgnore="visibility" 
    nodeKeyGenerator="MvcSiteMapProvider.DefaultNodeKeyGenerator, MvcSiteMapProvider"
    controllerTypeResolver="MvcSiteMapProvider.DefaultControllerTypeResolver, MvcSiteMapProvider" 
    actionMethodParameterResolver="MvcSiteMapProvider.DefaultActionMethodParameterResolver, 
    MvcSiteMapProvider" aclModule="MvcSiteMapProvider.DefaultAclModule, MvcSiteMapProvider" 
    siteMapNodeUrlResolver="MvcSiteMapProvider.DefaultSiteMapNodeUrlResolver, MvcSiteMapProvider" 
    siteMapNodeVisibilityProvider="MvcSiteMapProvider.DefaultSiteMapNodeVisibilityProvider, MvcSiteMapProvider" 
    siteMapProviderEventHandler="MvcSiteMapProvider.DefaultSiteMapProviderEventHandler, MvcSiteMapProvider" />
  </providers>
</siteMap>

and then using

@Html.MvcSiteMap("AdminSiteMapProvider").Menu()

But it is not working. Is someone having the same problem??????? Btw the error is:

Could not find file 'P:\MVCTest\MvcSite1\MvcSite1\Mvc.sitemap'. 

It is like they are just ignoring the web.config

1条回答
叛逆
2楼-- · 2019-08-25 10:41

These settings are ignored when using v4. MvcSiteMapProvider no longer uses Microsoft's sitemap provider model and the <siteMap> section can be removed from the web.config entirely.

To handle your requirement of 2 sitemaps in one application, you will need to use a dependency injection container and implement 1 or 2 custom classes to map incoming HTTP requests to a specific SiteMap instance.

The DI part can be handled by installing one of the MvcSiteMapProvider.MVC[x].DI.[ContainerName] packages.

ISiteMapCacheKeyGenerator provides the name of the "current" SiteMap based on the HTTP request, and ISiteMapCacheKeyToBuilderSetMapper tells MvcSiteMapProvider which builder to use (which can be configured for an XML file or other source).

See Multiple SiteMaps in One Application for a complete example of how this can be done.

查看更多
登录 后发表回答