You can use navigation controls to add site navigation to your pages with little or no code, but you can also work with site navigation programmatically. When your Web application runs, ASP.NET exposes a SiteMap object that reflects the site-map structure. All of the members of the SiteMap object are static. The SiteMap object, in turn, exposes a collection of SiteMapNode objects that contain properties for each node in the map. This is because, when you use the SiteMapPath control, the control works with the SiteMap and SiteMapNode objects to render the appropriate links automatically.
You can use the SiteMap, SiteMapNode, and SiteMapProvider objects in your own code to traverse the site-map structure or create a custom control to display site-map data. You cannot write to the site map, but you can alter site-map nodes in the instance of the object. For more information, see How to: Programmatically Modify Site-Map Nodes in Memory or How to: Programmatically Enumerate Site-Map Nodes.
ASP.NET uses the default site-map provider, XmlSiteMapProvider, to read the Web.sitemap file. If you want to store site-map information in a location other than the site-map file, you can create your own site-map provider and configure your application to call the custom provider. The site-map provider is configured in the Web.config file. When the application runs, ASP.NET will invoke your provider, which can retrieve site-map information as needed. ASP.NET then creates and populates the SiteMapNode objects based on the information that your provider returns. These objects can be programmatically accessed by using the SiteMap class. For more information, see Implementing ASP.NET Site-Map Providers.
Security Note: |
|---|
Implementing a custom site-map provider that stores site-map data in a file with a file name extension other than .sitemap is a potential security risk. By default, ASP.NET is configured to protect files with known file name extensions — such as .sitemap — from being downloaded by a client. To help protect your data, place any custom site-map data files that have a file name extension other than .sitemap in the App_Data folder. For more information, see Securing ASP.NET Site Navigation. |
When enabled, security trimming affects the behavior of some of the members in the SiteMap, SiteMapNode, and SiteMapNodeCollection classes. When using these classes, you will see the following behavior:
A null is returned by a site navigation API member if it attempts to reference a site-map node that the user does not have the security rights to see. For example, the CurrentNode, NextSibling, ParentNode, and PreviousSibling properties will return a null if the properties attempt to return a site-map node that is restricted.
If a site navigation API member needs to traverse the tree of site-map nodes, any site-map node that the user is not allowed to see is excluded from the traversal. For example, when the ChildNodes method runs, the collection of nodes is filtered to include only those nodes that the user is allowed to see. In the case of API members that need to keep track of node paths, such as the Clone or IsDescendantOf methods, the paths end at restricted nodes. This can result in cloning operations returning a reduced number of nodes. It can also result in the IsDescendantOf method returning a value of false even though structurally a node might actually be a descendant of the requested node.
An InvalidOperationException exception is returned if a site navigation API member references a root node that the user does not have the security rights to see. Only the root node of the root provider needs to be accessible to all users, which prevents an exception from being thrown when first obtaining the SiteMap object.
A ConfigurationException exception is thrown if a SiteMapNode object references another site-map file or provider incorrectly.
Note: |
|---|
In a site map, you can reference URLs that are outside of your Web application. Access to a URL outside of the application cannot be tested by ASP.NET. Therefore, if you enable security trimming, the site-map node will not be visible unless you set the roles attribute to an asterisk (*), which enables all clients to view the site-map node without first testing access to the URL. |