Changing SiteMap SiteMapProvider?

I have a custom menu navigation built from the web.sitemap file, the first line of which will look something like this:

SiteMapNodeCollection topLevelNodes = SiteMap.RootNode.ChildNodes;

This works - it gets all the top-level nodes from the web.sitemap file and allows me to browse every SiteMapNode and do things.

However, now I want to be able to create several web.sitemap files, and then programmatically determine which web.sitemap file to use, but I cannot figure out how to do this. I assume that I can either create one custom SiteMapProvider that can execute the logic to determine which web.sitemap file is loading, or I have several providers, each with the SiteMapFile property set to a specific * .sitemap file, and then switch providers programmatically before I access SiteMap.RootNode.

I think it might be easier to have one configurable provider and then override the part where it looks for the actual location of the physical map file, but I don’t understand how to do this

I searched a lot on googled, but most of the answers seem to relate to standard sitemappath controls, etc. and how to install SiteMapDataSource, which, it seems to me, has nothing to do with my approach.

+3
source share
2 answers

First you need to list all your Sitemaps in your web.config as such:

<siteMap defaultProvider="FNDSiteMap" enabled="true">
  <providers>
    <add name="FNDSiteMap" type="System.Web.XmlSiteMapProvider" siteMapFile="FND.sitemap" securityTrimmingEnabled="true"/>
    <add name="STASiteMap" type="System.Web.XmlSiteMapProvider" siteMapFile="STA.sitemap" securityTrimmingEnabled="true"/>
    <add name="TASiteMap" type="System.Web.XmlSiteMapProvider" siteMapFile="TA.sitemap" securityTrimmingEnabled="true"/>
  </providers>
</siteMap>

Then in your code, you can dynamically assign your SiteMapDataSource (which is tied to your menu) to one of the providers specified in your web.config:

.aspx

<asp:Menu ID="MenuLevel1" runat="server" Orientation="Horizontal" DataSourceID="SiteMapLevel1"
    MaximumDynamicDisplayLevels="0" IncludeStyleBlock="false">
</asp:Menu>                
<asp:SiteMapDataSource ID="SiteMapLevel1" runat="server" /> 

.cs

SiteMapLevel1.SiteMapProvider = "TASiteMap";
+3
source

Pauli's comment was an answer to my particular demand:

" / -... RootNode SiteMap.Providers[someProvider].RootNode, someProvider ."

, , .

+3

Source: https://habr.com/ru/post/1787775/


All Articles