共用方式為


Implementing ASP.NET Site-Map Providers

ASP.NET site navigation provides several Web server controls that display a navigation structure on a Web page: the SiteMapPath, TreeView, and Menu controls .These Web server controls use the ASP.NET default site-map provider, or the XMLSiteMapProvider class, to read site-map information from the XML-formatted Web.sitemap file.

Alternatively, you can implement your own site-map provider. Three primary reasons for creating a custom site-map provider are as follows:

  • To store site-map information in a data source that is not supported by the ASP.NET default site-map provider. For example, you might want to store your site-map data in a Visual FoxPro database, an Oracle database, or other data source.

  • To manage navigation information using a schema that is different from the schema used for the Web.sitemap file. For example, you might have an existing implementation for storing site-map data.

  • To use a dynamic site-map structure. For example, you might want each client account to be able to view a different site map.

Security noteSecurity 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.

Required Classes

To implement a site-map provider, create a class that inherits the SiteMapProvider abstract class from the System.Web namespace, and then implement the abstract members that are exposed by the SiteMapProvider class. This topic describes the required properties and methods that you must implement from the SiteMapProvider abstract classes. To review an implementation of each member, see the example code supplied for the Microsoft Access- and text-based site-map providers in How to: Implement ASP.NET Site-Map Providers.

NoteNote

The SiteMapProvider class imposes one constraint on the structure of the site-map data: only one root node can exist. The implementation of the default ASP.NET site-map provider also specifies that URLs must be unique within the scope of the provider, but custom site-map providers do not have this limitation.

Required SiteMapProvider Members

The following table lists the only methods that are required to implement a custom site-map provider. The SiteMapProvider base class provides a minimal implementation.

Method Description

FindSiteMapNode

Retrieves an instance of the SiteMapNode class, which represents a page. This method is overloaded.

GetChildNodes

Retrieves the child nodes of a specific instance of the SiteMapNode class.

GetParentNode

Retrieves the parent node of a specific instance of the SiteMapNode class.

GetRootNodeCore

Retrieves the root node of all the nodes that are managed by the current provider. This method is called internally by various site navigation classes to ensure that the navigation data has been loaded by the provider. This method must not return a null node.

If you are satisfied with the semantics of the XmlSiteMapProvider class but want to use a different data store than the Web.sitemap file, then you should derive from the StaticSiteMapProvider class instead of the SiteMapProvider class. Otherwise, some methods, such as the AddNode and RemoveNode methods, will throw a NotImplementedException unless they are implemented. The StaticSiteMapProvider class provides all of the internal logic for storing and searching nodes; you only need to implement the GetRootNodeCore method and the abstract BuildSiteMap method. The GetRootNodeCore method can simply call the BuildSiteMap method.

Custom Members

You might need to extend the site-map provider interfaces with additional functionality that is not provided by the SiteMapProvider abstract classes. If the custom provider is configured to be the default provider and the return value of Provider is cast to the type of the custom provider, then any public members that you add to your site-map provider can be accessed by using the Provider property of the SiteMap class.

See Also

Tasks

How to: Implement ASP.NET Site-Map Providers

Concepts

ASP.NET Site Navigation Overview
ASP.NET Site-Map Security Trimming
Securing ASP.NET Site Navigation
Securing Data Access

Other Resources

ASP.NET Application Security in Hosted Environments