Udostępnij za pośrednictwem


Example Text Site-Map Provider

Illustrates a complete text-based site-map provider.

Example

Description

The following code example demonstrates how to write a class that implements the abstract SiteMapProvider class.

Code

Imports System
Imports System.Collections
Imports System.Collections.Specialized
Imports System.Configuration.Provider
Imports System.IO
Imports System.Security.Permissions
Imports System.Web

Namespace Samples.AspNet.VB

  <AspNetHostingPermission(SecurityAction.Demand, Level:=AspNetHostingPermissionLevel.Minimal)> _
  PublicClass SimpleTextSiteMapProvider
    Inherits SiteMapProvider

    Private parentSiteMapProvider As SiteMapProvider = NothingPrivate simpleTextProviderName AsString = NothingPrivate sourceFilename AsString = NothingPrivate aRootNode As SiteMapNode = NothingPrivate siteMapNodes As ArrayList = NothingPrivate childParentRelationship As ArrayList = Nothing
    ' A default constructor. The Name property is initialized in the    ' Initialize method.PublicSubNew()
    EndSub 'New
    ' Implement the CurrentNode property.PublicOverridesReadOnlyProperty CurrentNode() As SiteMapNode
      GetDim currentUrl AsString = FindCurrentUrl()
        ' Find the SiteMapNode that represents the current page.Dim aCurrentNode As SiteMapNode = FindSiteMapNode(currentUrl)
        Return aCurrentNode
      EndGetEndProperty
    ' Implement the RootNode property.PublicOverridesReadOnlyProperty RootNode() As SiteMapNode
      GetReturn aRootNode
      EndGetEndProperty
    ' Implement the ParentProvider property.PublicOverridesProperty ParentProvider() As SiteMapProvider
      GetReturn parentSiteMapProvider
      EndGetSet(ByVal value As SiteMapProvider)
        parentSiteMapProvider = Value
      EndSetEndProperty
    ' Implement the RootProvider property.PublicOverridesReadOnlyProperty RootProvider() As SiteMapProvider
      Get        ' If the current instance belongs to a provider hierarchy, it        ' cannot be the RootProvider. Rely on the ParentProvider.IfNot (Me.ParentProvider IsNothing) ThenReturn ParentProvider.RootProvider
          ' If the current instance does not have a ParentProvider, it is          ' not a child in a hierarchy, and can be the RootProvider.ElseReturnMeEndIfEndGetEndProperty
    ' Implement the FindSiteMapNode method.PublicOverridesFunction FindSiteMapNode(ByVal rawUrl AsString) As SiteMapNode
      ' Does the root node match the URL?If RootNode.Url = rawUrl ThenReturn RootNode
      ElseDim candidate As SiteMapNode = Nothing        ' Retrieve the SiteMapNode that matches the URL.SyncLockMe
          candidate = GetNode(siteMapNodes, rawUrl)
        EndSyncLockReturn candidate
      EndIfEndFunction 'FindSiteMapNode

    ' Implement the GetChildNodes method.PublicOverridesFunction GetChildNodes(ByVal node As SiteMapNode) As SiteMapNodeCollection
      Dim children AsNew SiteMapNodeCollection()
      ' Iterate through the ArrayList and find all nodes that have the specified node as a parent.SyncLockMeDim i AsIntegerFor i = 0 To childParentRelationship.Count - 1

          Dim de As DictionaryEntry = CType(childParentRelationship(i), DictionaryEntry)
          Dim nodeUrl AsString = CType(de.Key, String)

          Dim parent As SiteMapNode = GetNode(childParentRelationship, nodeUrl)

          IfNot (parent IsNothing) AndAlso node.Url = parent.Url Then            ' The SiteMapNode with the Url that corresponds to nodeUrl            ' is a child of the specified node. Get the SiteMapNode for            ' the nodeUrl.Dim child As SiteMapNode = FindSiteMapNode(nodeUrl)
            IfNot (child IsNothing) Then
              children.Add(CType(child, SiteMapNode))
            ElseThrowNew Exception("ArrayLists not in sync.")
            EndIfEndIfNext i
      EndSyncLockReturn children
    EndFunction 'GetChildNodes

    ProtectedOverridesFunction GetRootNodeCore() As SiteMapNode
      Return RootNode
    EndFunction ' GetRootNodeCore()

    ' Implement the GetParentNode method.PublicOverridesFunction GetParentNode(ByVal node As SiteMapNode) As SiteMapNode
      ' Check the childParentRelationship table and find the parent of the current node.      ' If there is no parent, the current node is the RootNode.Dim parent As SiteMapNode = NothingSyncLockMe        ' Get the Value of the node in childParentRelationship
        parent = GetNode(childParentRelationship, node.Url)
      EndSyncLockReturn parent
    EndFunction 'GetParentNode

    ' Implement the ProviderBase.Initialize method.    ' Initialize is used to initialize the state that the Provider holds, but    ' not actually build the site map.PublicOverridesSub Initialize(ByVal name AsString, ByVal attributes As NameValueCollection)
      SyncLockMeMyBase.Initialize(name, attributes)
        simpleTextProviderName = name
        sourceFilename = attributes("siteMapFile")
        siteMapNodes = New ArrayList()
        childParentRelationship = New ArrayList()
        ' Build the site map in memory.
        LoadSiteMapFromStore()
      EndSyncLockEndSub 'Initialize

    ' Private helper methodsPrivateFunction GetNode(ByVal list As ArrayList, ByVal url AsString) As SiteMapNode
      Dim i AsIntegerFor i = 0 To list.Count - 1
        Dim item As DictionaryEntry = CType(list(i), DictionaryEntry)
        IfCStr(item.Key) = url ThenReturnCType(item.Value, SiteMapNode)
        EndIfNext i
      ReturnNothingEndFunction 'GetNode


    ' Get the URL of the currently displayed page.PrivateFunction FindCurrentUrl() AsStringTry        ' The current HttpContext.Dim currentContext As HttpContext = HttpContext.Current
        IfNot (currentContext IsNothing) ThenReturn currentContext.Request.RawUrl
        ElseThrowNew Exception("HttpContext.Current is Invalid")
        EndIfCatch e As Exception
        ThrowNew NotSupportedException("This provider requires a valid context.", e)
      EndTryEndFunction 'FindCurrentUrl

    ProtectedOverridableSub LoadSiteMapFromStore()
      Dim pathToOpen AsStringSyncLockMe        ' If a root node exists, LoadSiteMapFromStore has already        ' been called, and the method can return.IfNot (aRootNode IsNothing) ThenReturnElse
          pathToOpen = HttpContext.Current.Server.MapPath("~" & "\\" & sourceFilename)
          If File.Exists(pathToOpen) Then            ' Open the file to read from.Dim sr As StreamReader = File.OpenText(pathToOpen)
            Try
              ' Clear the state of the collections and aRootNode
              aRootNode = Nothing
              siteMapNodes.Clear()
              childParentRelationship.Clear()

              ' Parse the file and build the site mapDim s AsString = ""Dim nodeValues AsString() = NothingDim temp As SiteMapNode = NothingDo
                s = sr.ReadLine()

                IfNot s IsNothingThen                  ' Build the various SiteMapNode objects and add                  ' them to the ArrayList collections. The format used                  ' is: URL,TITLE,DESCRIPTION,PARENTURL
                  nodeValues = s.Split(","c)

                  temp = New SiteMapNode(Me, _
                      HttpRuntime.AppDomainAppVirtualPath & "/" & nodeValues(0), _
                      HttpRuntime.AppDomainAppVirtualPath & "/" & nodeValues(0), _
                      nodeValues(1), _
                      nodeValues(2))

                  ' Is this a root node yet?If aRootNode IsNothingAndAlso _
                    (nodeValues(3) IsNothingOrElse _
                     nodeValues(3) = String.Empty) Then
                    aRootNode = temp

                    ' If not the root node, add the node to the various collections.Else

                    siteMapNodes.Add(New DictionaryEntry(temp.Url, temp))

                    ' The parent node has already been added to the collection.Dim parentNode As SiteMapNode = _
                        FindSiteMapNode(HttpRuntime.AppDomainAppVirtualPath & "/" & nodeValues(3))

                    IfNot (parentNode IsNothing) Then
                      childParentRelationship.Add(New DictionaryEntry(temp.Url, parentNode))
                    ElseThrowNew Exception("Parent node not found for current node.")
                    EndIfEndIfEndIfLoopUntil s IsNothingFinally
              sr.Close()
            EndTryElseThrowNew Exception("File not found")
          EndIfEndIfEndSyncLockReturnEndSub 'LoadSiteMapFromStore
  EndClass 'SimpleTextSiteMapProvider
EndNamespace
using System;
using System.Configuration.Provider;
using System.Collections;
using System.Collections.Specialized;
using System.IO;
using System.Security.Permissions;
using System.Web;

namespace Samples.AspNet.CS
{

  [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
  publicclass SimpleTextSiteMapProvider : SiteMapProvider
  {
    private SiteMapProvider parentSiteMapProvider = null;
    privatestring simpleTextProviderName = null;
    privatestring sourceFilename = null;
    private SiteMapNode rootNode = null;
    private ArrayList siteMapNodes = null;
    private ArrayList childParentRelationship = null;


    // A default constructor. The Name property is initialized in the// Initialize method.public SimpleTextSiteMapProvider()
    {
    }
    // Implement the CurrentNode property.publicoverride SiteMapNode CurrentNode
    {
      get
      {
        string currentUrl = FindCurrentUrl();
        // Find the SiteMapNode that represents the current page.
        SiteMapNode currentNode = FindSiteMapNode(currentUrl);
        return currentNode;
      }
    }

    // Implement the RootNode property.publicoverride SiteMapNode RootNode
    {
      get
      {
        return rootNode;
      }
    }
    // Implement the ParentProvider property.publicoverride SiteMapProvider ParentProvider
    {
      get
      {
        return parentSiteMapProvider;
      }
      set
      {
        parentSiteMapProvider = value;
      }
    }

    // Implement the RootProvider property.publicoverride SiteMapProvider RootProvider
    {
      get
      {
        // If the current instance belongs to a provider hierarchy, it// cannot be the RootProvider. Rely on the ParentProvider.if (this.ParentProvider != null)
        {
          return ParentProvider.RootProvider;
        }
        // If the current instance does not have a ParentProvider, it is// not a child in a hierarchy, and can be the RootProvider.else
        {
          returnthis;
        }
      }
    }
    // Implement the FindSiteMapNode method.publicoverride SiteMapNode FindSiteMapNode(string rawUrl)
    {

      // Does the root node match the URL?if (RootNode.Url == rawUrl)
      {
        return RootNode;
      }
      else
      {
        SiteMapNode candidate = null;
        // Retrieve the SiteMapNode that matches the URL.lock (this)
        {
          candidate = GetNode(siteMapNodes, rawUrl);
        }
        return candidate;
      }
    }
    // Implement the GetChildNodes method.publicoverride SiteMapNodeCollection GetChildNodes(SiteMapNode node)
    {
      SiteMapNodeCollection children = new SiteMapNodeCollection();
      // Iterate through the ArrayList and find all nodes that have the specified node as a parent.lock (this)
      {
        for (int i = 0; i < childParentRelationship.Count; i+)
        {

          string nodeUrl = ((DictionaryEntry)childParentRelationship[i]).Key asstring;

          SiteMapNode parent = GetNode(childParentRelationship, nodeUrl);

          if (parent != null && node.Url == parent.Url)
          {
            // The SiteMapNode with the Url that corresponds to nodeUrl// is a child of the specified node. Get the SiteMapNode for// the nodeUrl.
            SiteMapNode child = FindSiteMapNode(nodeUrl);
            if (child != null)
            {
              children.Add(child as SiteMapNode);
            }
            else
            {
              thrownew Exception("ArrayLists not in sync.");
            }
          }
        }
      }
      return children;
    }
    protectedoverride SiteMapNode GetRootNodeCore()
    {
      return RootNode;
    }
    // Implement the GetParentNode method.publicoverride SiteMapNode GetParentNode(SiteMapNode node)
    {
      // Check the childParentRelationship table and find the parent of the current node.// If there is no parent, the current node is the RootNode.
      SiteMapNode parent = null;
      lock (this)
      {
        // Get the Value of the node in childParentRelationship
        parent = GetNode(childParentRelationship, node.Url);
      }
      return parent;
    }

    // Implement the ProviderBase.Initialize property.// Initialize is used to initialize the state that the Provider holds, but// not actually build the site map.publicoverridevoid Initialize(string name, NameValueCollection attributes)
    {

      lock (this)
      {

        base.Initialize(name, attributes);

        simpleTextProviderName = name;
        sourceFilename = attributes["siteMapFile"];
        siteMapNodes = new ArrayList();
        childParentRelationship = new ArrayList();

        // Build the site map in memory.
        LoadSiteMapFromStore();
      }
    }
    // Private helper methodsprivate SiteMapNode GetNode(ArrayList list, string url)
    {
      for (int i = 0; i < list.Count; i+)
      {
        DictionaryEntry item = (DictionaryEntry)list[i];
        if ((string)item.Key == url)
          return item.Value as SiteMapNode;
      }
      returnnull;
    }

    // Get the URL of the currently displayed page.privatestring FindCurrentUrl()
    {
      try
      {
        // The current HttpContext.
        HttpContext currentContext = HttpContext.Current;
        if (currentContext != null)
        {
          return currentContext.Request.RawUrl;
        }
        else
        {
          thrownew Exception("HttpContext.Current is Invalid");
        }
      }
      catch (Exception e)
      {
        thrownew NotSupportedException("This provider requires a valid context.",e);
      }
    }
    protectedvirtualvoid LoadSiteMapFromStore()
    {
      string pathToOpen;

      lock (this)
      {
        // If a root node exists, LoadSiteMapFromStore has already// been called, and the method can return.if (rootNode != null)
        {
          return;
        }
        else
        {
          pathToOpen = HttpContext.Current.Server.MapPath("~" + "\\" + sourceFilename);

          if (File.Exists(pathToOpen))
          {
            // Open the file to read from.using (StreamReader sr = File.OpenText(pathToOpen))
            {

              // Clear the state of the collections and rootNode
              rootNode = null;
              siteMapNodes.Clear();
              childParentRelationship.Clear();

              // Parse the file and build the site mapstring s = "";
              string[] nodeValues = null;
              SiteMapNode temp = null;

              while ((s = sr.ReadLine()) != null)
              {

                // Build the various SiteMapNode objects and add// them to the ArrayList collections. The format used// is: URL,TITLE,DESCRIPTION,PARENTURL

                nodeValues = s.Split(',');

                temp = new SiteMapNode(this,
                    HttpRuntime.AppDomainAppVirtualPath + "/" + nodeValues[0],
                    HttpRuntime.AppDomainAppVirtualPath + "/" + nodeValues[0],
                    nodeValues[1],
                    nodeValues[2]);

                // Is this a root node yet?if (null == rootNode &&
                    (null == nodeValues[3] || nodeValues[3] == String.Empty))
                {
                  rootNode = temp;
                }

              // If not the root node, add the node to the various collections.else
                {
                  siteMapNodes.Add(new DictionaryEntry(temp.Url, temp));
                  // The parent node has already been added to the collection.
                  SiteMapNode parentNode =
                           FindSiteMapNode(HttpRuntime.AppDomainAppVirtualPath + "/" + nodeValues[3]);
                  if (parentNode != null)
                  {
                    childParentRelationship.Add(new DictionaryEntry(temp.Url, parentNode));
                  }
                  else
                  {
                    thrownew Exception("Parent node not found for current node.");
                  }
                }
              }
            }
          }
          else
          {
            thrownew Exception("File not found");
          }
        }
      }
      return;
    }
  }

}

Comments

The code example uses a comma-delimited file called SiteMap.txt that follows a specific structure to load site-map information. The first line of the file represents the root node of the site map, and subsequent lines are child nodes. Each child node identifies its parent node by URL.

default.aspx,Home,MyCompany Home Page,
sale.aspx,Now On Sale,Check Out These Great Deals!,default.aspx
catalog.aspx,Online Catalog,Browse Our Many Great Items!,default.aspx

The SimpleTextSiteMapProvider provides example implementations of all of the properties and methods for the SiteMapProvider class.

Finally, the SimpleTextSiteMapProvider is configured to be the default provider in the Web.config file, as shown in the following code example.

<configuration>
  <system.web>
    <siteMap defaultProvider="SimpleTextSiteMapProvider">
     <providers>
       <add 
         name="SimpleTextSiteMapProvider"
         type="<type name>"
         siteMapFile = "<path>/siteMap.txt" />
     </providers> 
    </siteMap>
  </system.web>
</configuration>

To customize this example, replace <type name> with the fully qualified name of the class that implements your site-map data provider. For example, in the C# code above, you would replace <type name> with Samples.AspNet.CS.SimpleTextSiteMapProvider. If you compile your site-map data provider code and place it in the Bin directory, the <type name> string must also include the name of your compiled file without the file name extension. For example, if you compiled the C# code above into a file called Samples.AspNet.dll, you would replace <type name> with Samples.AspNet.CS.SimpleTextSiteMapProvider.Samples.AspNet. Lastly, replace <path> with the relative path to your site-map file.

Note

When you inherit from the SiteMapProvider class, as opposed to inheriting from the StaticSiteMapProvider class, you must override the following members: GetRootNodeCore, FindSiteMapNode, GetChildNodes, and GetParentNode.

See Also

Concepts

ASP.NET Site Navigation Overview

Securing ASP.NET Site Navigation

Securing Data Access

Reference

SiteMapProvider

StaticSiteMapProvider

SiteMapNode

SiteMap

Other Resources

ASP.NET Application Security in Hosted Environments