Partager via


How to: Implement ASP.NET Site-Map Providers

This topic describes how you can build ASP.NET site-map providers and configure an ASP.NET application to use a site-map provider.

The following table lists the accompanying topics, which include the code for two example site-map providers written in Visual Basic and C#. One of the example providers uses the .NET Framework Data Provider for ODBC to connect to an ODBC data source. The other example uses a Microsoft Access database as its data source.

Example Text Site-Map Provider

Illustrates a complete text-based site-map provider.

Example Access Site-Map Provider

Illustrates a complete Access-based site-map provider.

To build a site-map provider

  • Place your source code in the App_Code directory of your application.

    Note

    If you already have source code in the App_Code directory of your application, you must add the version of the site-map provider that is written in the same language as the existing code in the directory.

    The provider will be compiled by ASP.NET when your application is requested. For more information, see Shared Code Folders in ASP.NET Web Site Projects.

    --or--

    Alternatively, compile the site-map provider as a library and place it in the Bin directory of your Web application, or strongly name it and place it in the global assembly cache (GAC). For example, the following command shows how you can compile an example site-map provider using the command-line compiler.

    vbc /out:<example_name>.dll /t:library <example_name>.vb /r:System.Web.dll /r:System.Configuration.dll
    
    csc /out:<example_name>.dll /t:library <example_name>.cs /r:System.Web.dll /r:System.Configuration.dll
    

To use the site-map provider in an ASP.NET application

  1. Configure your Web application to use the site-map provider, adding the provider to your Web.config file.

  2. Add controls that use the site-map provider. After the Web.config file is altered and the provider is compiled, the provider loads navigation data into the instance of the SiteMap class that is in memory. Now the navigation data can be used by components of the site-map infrastructure — such as the SiteMapPath, TreeView, and Menu controls — to display site-map information for users. The following example code uses all three of these controls in an ASP.NET page.

    <%@ Page Language="VB" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
    
    </script>
    
    <html xmlns="https://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">
      <title>Simple Navigation Controls</title>
    </head>
    <body>
      <form id="form1" runat="server">
      <div>
    
      <h2>Using SiteMapPath</h2>
      <asp:SiteMapPath ID="SiteMapPath1" Runat="server">
      </asp:SiteMapPath>
    
    
      <asp:SiteMapDataSource ID="SiteMapDataSource1" Runat="server" />
    
      <h2>Using TreeView</h2>
      <asp:TreeView ID="TreeView1" Runat="Server" DataSourceID="SiteMapDataSource1">
      </asp:TreeView>
    
      <h2>Using Menu</h2>
      <asp:Menu ID="Menu2" Runat="server" DataSourceID="SiteMapDataSource1">
      </asp:Menu>
    
      <h2>Using a Horizontal Menu</h2>
      <asp:Menu ID="Menu1" Runat="server" DataSourceID="SiteMapDataSource1"
        Orientation="Horizontal" 
        StaticDisplayLevels="2" >
      </asp:Menu>
    
      </div>
      </form>
    </body>
    </html>
    
    <%@ Page Language="C#" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
    </script>
    
    <html xmlns="https://www.w3.org/1999/xhtml" >
    <head runat="server">
      <title>Simple Navigation Controls</title>
    </head>
    <body>
      <form id="form1" runat="server">
      <div>
    
      <h2>Using SiteMapPath</h2>
      <asp:SiteMapPath ID="SiteMapPath1" Runat="server">
      </asp:SiteMapPath>
    
    
      <asp:SiteMapDataSource ID="SiteMapDataSource1" Runat="server" />
    
      <h2>Using TreeView</h2>
      <asp:TreeView ID="TreeView1" Runat="Server" DataSourceID="SiteMapDataSource1">
      </asp:TreeView>
    
      <h2>Using Menu</h2>
      <asp:Menu ID="Menu2" Runat="server" DataSourceID="SiteMapDataSource1">
      </asp:Menu>
    
      <h2>Using a Horizontal Menu</h2>
      <asp:Menu ID="Menu1" Runat="server" DataSourceID="SiteMapDataSource1"
        Orientation="Horizontal" 
        StaticDisplayLevels="2" >
      </asp:Menu>
    
      </div>
      </form>
    </body>
    </html>
    

See Also

Concepts

Implementing ASP.NET Site-Map Providers

Securing ASP.NET Site Navigation

Securing Data Access in ASP.NET

Other Resources

ASP.NET Site Navigation

ASP.NET Application Security in Hosted Environments