How do you make 1 SiteMap per MVC area and use MvcSiteMapNodeAttribute at the same time?
相关问题
- MVC-Routing,Why i can not ignore defaults,The matc
- parameters in routing do not work MVC 3
- There is no ViewData item with the key 'taskTy
- TextBoxFor decimal
- Install ASP.NET 5.0 version of System.ServiceModel
相关文章
- How to get a list of connected clients on SignalR
- How do you redirect to the calling page in ASP.NET
- Change color of bars depending on value in Highcha
- The program '[4432] iisexpress.exe' has ex
- ASP.Net MVC 4 Bundles
- How to get server path of physical path ?
- Cannot implicitly convert Web.Http.Results.JsonRes
- entity type has no key defined - Code first
Please have a look at this answer for help with setting up MvcSiteMapProvider with areas. The routes have to be configured using the correct conventions or it won't work right.
However, that alone isn't going to address this requirement, because there is no default assumption made that you want to have a different SiteMap per area.
The behavior of the internal DI container assumes that there will be 1 SiteMap per domain name, and that all of the SiteMaps in the application will be built using the same configuration. There is no way to change this behavior unless you use an external DI container and follow the instructions in Multiple SiteMaps in One Application to override it.
Option 1
You could continue using the internal DI container and a single SiteMap for the entire website and you could create a custom ISiteMapNodeVisibilityProvider that hides everything that is not in the current area by reading the area from the current request.
Then set it up as the default visibility provider.
Using external DI (StructureMap example shown):
Do note that you will still need to nest your area nodes below the non-area part of the site if you do this, so it might not behave as you would like. You need to ensure you set the parent key of the Admin area to a key of a node in the non-area part - there can only be 1 root node per SiteMap.
Also, if you go this route, be sure to set the MvcSiteMapProvider_VisibilityAffectsDescendants setting to "false" so your area nodes are not affected by the visibility of the non-area nodes.
Option 2
Inject a custom ISiteMapCacheKeyGenerator that is based on area and use the SiteMapCacheKey property of [MvcSiteMapNode] attribute to control which area the node belongs to.
You need to inject this using external DI (StructureMap example shown):
And then configure your [MvcSiteMapNode] attributes:
Option 3
Rather than setting SiteMapCacheKey on every [MvcSiteMapNode] attribute, you could put each area in a separate assembly and configure it to only scan the pertinent area assembly for [MvcSiteMapNode] attribute.
In the external DI module (StructureMap example shown):