StaticSiteMapProvider.BuildSiteMap Méthode

Définition

En cas de substitution dans une classe dérivée, charge les informations de plan de site à partir d'un stockage persistant et le génère en mémoire.

public:
 abstract System::Web::SiteMapNode ^ BuildSiteMap();
public abstract System.Web.SiteMapNode BuildSiteMap ();
abstract member BuildSiteMap : unit -> System.Web.SiteMapNode
Public MustOverride Function BuildSiteMap () As SiteMapNode

Retours

SiteMapNode

Le SiteMapNode racine de la structure de navigation du plan de site.

Exemples

L’exemple de code suivant montre comment implémenter la BuildSiteMap méthode pour récupérer des données à partir d’une base de données Microsoft Access et générer SiteMapNode des objets ajoutés à la ChildNodes collection du nœud de mappage de site racine. Enfin, la RootNode propriété est retournée à l’appelant.

Cet exemple de code fait partie d’un exemple plus grand fourni pour la StaticSiteMapProvider classe.

// Build an in-memory representation from persistent
// storage, and return the root node of the site map.
virtual SiteMapNode ^ BuildSiteMap() override
{
   // Since the SiteMap class is static, make sure that it is
   // not modified while the site map is built.
   System::Threading::Monitor::Enter( this );
   try
   {
      
      // If there is no initialization, this method is being
      // called out of order.
      if (  !IsInitialized )
      {
         throw gcnew Exception( "BuildSiteMap called incorrectly." );
      }
      
      // If there is no root node, then there is no site map.
      if ( nullptr == rootNode )
      {
         
         // Start with a clean slate
         Clear();
         
         // Select the root node of the site map from Microsoft Access.
         int rootNodeId = -1;
         if ( accessConnection->State == ConnectionState::Closed )
            accessConnection->Open();
         
         OleDbCommand^ rootNodeCommand = gcnew OleDbCommand
            ("SELECT nodeid, url, name FROM SiteMap WHERE parentnodeid IS NULL", accessConnection);
         OleDbDataReader^ rootNodeReader = rootNodeCommand->ExecuteReader();
         if ( rootNodeReader->HasRows )
         {
            rootNodeReader->Read();
            rootNodeId = rootNodeReader->GetInt32( 0 );
            
            // Create a SiteMapNode that references the current StaticSiteMapProvider.
            rootNode = gcnew SiteMapNode(this, rootNodeId.ToString(), 
               rootNodeReader->GetString( 1 ),rootNodeReader->GetString( 2 ));
         }
         else
            return nullptr;
         rootNodeReader->Close();
         
         // Select the child nodes of the root node.
         OleDbCommand^ childNodesCommand = gcnew OleDbCommand
            ("SELECT nodeid, url, name FROM SiteMap WHERE parentnodeid = ?", accessConnection);
         OleDbParameter^ rootParam = gcnew OleDbParameter( "parentid", OleDbType::Integer);
         rootParam->Value = rootNodeId;
         childNodesCommand->Parameters->Add( rootParam );
         OleDbDataReader^ childNodesReader = childNodesCommand->ExecuteReader();
         if ( childNodesReader->HasRows )
         {
            SiteMapNode ^ childNode = nullptr;
            while ( childNodesReader->Read() )
            {
               childNode = gcnew SiteMapNode( this, 
                   System::Convert::ToString(childNodesReader->GetInt32( 0 )),
                  childNodesReader->GetString( 1 ),
                  childNodesReader->GetString( 2 ) 
               );
               // Use the SiteMapNode AddNode method to add
               // the SiteMapNode to the ChildNodes collection.
               AddNode( childNode, rootNode );
            }
         }
         childNodesReader->Close();
         accessConnection->Close();
      }
      return rootNode;
   }
   finally
   {
      System::Threading::Monitor::Exit( this );
   }

}
// Build an in-memory representation from persistent
// storage, and return the root node of the site map.
public override SiteMapNode BuildSiteMap() {

    // Since the SiteMap class is static, make sure that it is
    // not modified while the site map is built.
    lock(this) {

        // If there is no initialization, this method is being
        // called out of order.
        if (! IsInitialized) {
            throw new Exception("BuildSiteMap called incorrectly.");
        }

        // If there is no root node, then there is no site map.
        if (null == rootNode) {
            // Start with a clean slate
            Clear();

            // Select the root node of the site map from Microsoft Access.
            int rootNodeId = -1;

            if (accessConnection.State == ConnectionState.Closed)
                accessConnection.Open();
            OleDbCommand rootNodeCommand =
                new OleDbCommand("SELECT nodeid, url, name FROM SiteMap WHERE parentnodeid IS NULL",
                                 accessConnection);
            OleDbDataReader rootNodeReader = rootNodeCommand.ExecuteReader();

            if(rootNodeReader.HasRows) {
                rootNodeReader.Read();
                rootNodeId = rootNodeReader.GetInt32(0);
                // Create a SiteMapNode that references the current StaticSiteMapProvider.
                rootNode   = new SiteMapNode(this,
                                             rootNodeId.ToString(),
                                             rootNodeReader.GetString(1),
                                             rootNodeReader.GetString(2));
            }
            else
            {
                return null;
            }

            rootNodeReader.Close();
            // Select the child nodes of the root node.
            OleDbCommand childNodesCommand =
                new OleDbCommand("SELECT nodeid, url, name FROM SiteMap WHERE parentnodeid = ?",
                                 accessConnection);
            OleDbParameter rootParam = new OleDbParameter("parentid", OleDbType.Integer);
            rootParam.Value = rootNodeId;
            childNodesCommand.Parameters.Add(rootParam);

            OleDbDataReader childNodesReader = childNodesCommand.ExecuteReader();

            if (childNodesReader.HasRows) {

                SiteMapNode childNode = null;
                while(childNodesReader.Read()) {
                    childNode =  new SiteMapNode(this,
                                                 childNodesReader.GetInt32(0).ToString(),
                                                 childNodesReader.GetString(1),
                                                 childNodesReader.GetString(2));

                    // Use the SiteMapNode AddNode method to add
                    // the SiteMapNode to the ChildNodes collection.
                    AddNode(childNode, rootNode);
                }
            }

            childNodesReader.Close();
            accessConnection.Close();
        }
        return rootNode;
    }
}
' Build an in-memory representation from persistent
' storage, and return the root node of the site map.
Public Overrides Function BuildSiteMap() As SiteMapNode

    ' Since the SiteMap class is static, make sure that it is
    ' not modified while the site map is built.
    SyncLock Me

        ' If there is no initialization, this method is being
        ' called out of order.
        If Not IsInitialized Then
            Throw New Exception("BuildSiteMap called incorrectly.")
        End If

        ' If there is no root node, then there is no site map.
        If aRootNode Is Nothing Then
            ' Start with a clean slate
            Clear()

            ' Select the root node of the site map from Microsoft Access.
            Dim rootNodeId As Integer = -1

            If accessConnection.State = ConnectionState.Closed Then
                accessConnection.Open()
            End If
            Dim rootNodeCommand As New OleDbCommand("SELECT nodeid, url, name FROM SiteMap WHERE parentnodeid IS NULL", accessConnection)
            Dim rootNodeReader As OleDbDataReader = rootNodeCommand.ExecuteReader()

            If rootNodeReader.HasRows Then
                rootNodeReader.Read()
                rootNodeId = rootNodeReader.GetInt32(0)
                ' Create a SiteMapNode that references the current StaticSiteMapProvider.
                aRootNode = New SiteMapNode(Me, rootNodeId.ToString(), rootNodeReader.GetString(1), rootNodeReader.GetString(2))
            Else
                Return Nothing
            End If
            rootNodeReader.Close()
            ' Select the child nodes of the root node.
            Dim childNodesCommand As New OleDbCommand("SELECT nodeid, url, name FROM SiteMap WHERE parentnodeid = ?", accessConnection)
            Dim rootParam As New OleDbParameter("parentid", OleDbType.Integer)
            rootParam.Value = rootNodeId
            childNodesCommand.Parameters.Add(rootParam)

            Dim childNodesReader As OleDbDataReader = childNodesCommand.ExecuteReader()

            If childNodesReader.HasRows Then

                Dim childNode As SiteMapNode = Nothing
                While childNodesReader.Read()
                    childNode = New SiteMapNode(Me, _
                    childNodesReader.GetInt32(0).ToString(), _
                    childNodesReader.GetString(1), _
                    childNodesReader.GetString(2))

                    ' Use the SiteMapNode AddNode method to add
                    ' the SiteMapNode to the ChildNodes collection.
                    AddNode(childNode, aRootNode)
                End While
            End If

            childNodesReader.Close()
            accessConnection.Close()
        End If
        Return aRootNode
    End SyncLock

End Function 'BuildSiteMap

Remarques

La BuildSiteMap méthode est le membre abstrait de la StaticSiteMapProvider classe. La BuildSiteMap méthode est appelée pour charger et générer le nœud de carte de site à partir du stockage persistant. Lorsque vous implémentez la BuildSiteMap méthode, assurez-vous qu’elle est sécurisée par thread, car plusieurs demandes de page simultanées peuvent entraîner indirectement plusieurs appels pour charger des informations de carte de site.

Notes pour les responsables de l’implémentation

Lors de la substitution de la BuildSiteMap() méthode dans une classe dérivée, veillez à normaliser les URL des SiteMapNode objets que vous ajoutez à votre fournisseur de cartes de site afin que la FindSiteMapNode(String) méthode puisse récupérer un nœud de carte de site, que l’URL du nœud de carte de site soit fournie en tant que chemin d’accès virtuel absolu ou chemin relatif à l’application. Les implémenteurs de fournisseur de cartes de site utilisant la AddNode méthode doivent normaliser les URL avant de SiteMapNode stocker des objets dans les tables de hachage internes pour le compte du fournisseur de carte de site.

Le comportement de découpage de sécurité est inclus dans les implémentations de classe et StaticSiteMapProvider de SiteMapProvider classe. Toutefois, pour que le découpage de sécurité fonctionne dans les classes dérivées, vous devez définir la Roles propriété de tous les SiteMapNode objets que vous créez lors de la création d’un fournisseur de carte de site lorsque vous remplacez la BuildSiteMap() méthode.

S’applique à

Voir aussi