Edit

Share via


SiteMapProvider.FindSiteMapNode Method

Definition

When overridden in a derived class, retrieves a SiteMapNode object that represents a page.

Overloads

FindSiteMapNode(String)

When overridden in a derived class, retrieves a SiteMapNode object that represents the page at the specified URL.

FindSiteMapNode(HttpContext)

Retrieves a SiteMapNode object that represents the currently requested page using the specified HttpContext object.

FindSiteMapNode(String)

When overridden in a derived class, retrieves a SiteMapNode object that represents the page at the specified URL.

public:
 abstract System::Web::SiteMapNode ^ FindSiteMapNode(System::String ^ rawUrl);
public abstract System.Web.SiteMapNode FindSiteMapNode (string rawUrl);
abstract member FindSiteMapNode : string -> System.Web.SiteMapNode
Public MustOverride Function FindSiteMapNode (rawUrl As String) As SiteMapNode

Parameters

rawUrl
String

A URL that identifies the page for which to retrieve a SiteMapNode.

Returns

A SiteMapNode that represents the page identified by rawURL; otherwise, null, if no corresponding SiteMapNode is found or if security trimming is enabled and the SiteMapNode cannot be returned for the current user.

Examples

The following code example demonstrates how to implement the FindSiteMapNode method in a class that implements the abstract SiteMapProvider class. The SimpleTextSiteMapProvider uses a helper method, named FindUrl, to get the URL of the currently displayed page from the HttpContext object.

This code example is part of a larger example provided for the SiteMapProvider class.

// Implement the FindSiteMapNode method.
public override 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 FindSiteMapNode method.
Public Overrides Function FindSiteMapNode(ByVal rawUrl As String) As SiteMapNode
  ' Does the root node match the URL?
  If RootNode.Url = rawUrl Then
    Return RootNode
  Else
    Dim candidate As SiteMapNode = Nothing
    ' Retrieve the SiteMapNode that matches the URL.
    SyncLock Me
      candidate = GetNode(siteMapNodes, rawUrl)
    End SyncLock
    Return candidate
  End If
End Function 'FindSiteMapNode
private 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;
  }
  return null;
}

// Get the URL of the currently displayed page.
private string FindCurrentUrl()
{
  try
  {
    // The current HttpContext.
    HttpContext currentContext = HttpContext.Current;
    if (currentContext != null)
    {
      return currentContext.Request.RawUrl;
    }
    else
    {
      throw new Exception("HttpContext.Current is Invalid");
    }
  }
  catch (Exception e)
  {
    throw new NotSupportedException("This provider requires a valid context.",e);
  }
}
Private Function GetNode(ByVal list As ArrayList, ByVal url As String) As SiteMapNode
  Dim i As Integer
  For i = 0 To list.Count - 1
    Dim item As DictionaryEntry = CType(list(i), DictionaryEntry)
    If CStr(item.Key) = url Then
      Return CType(item.Value, SiteMapNode)
    End If
  Next i
  Return Nothing
End Function 'GetNode


' Get the URL of the currently displayed page.
Private Function FindCurrentUrl() As String
  Try
    ' The current HttpContext.
    Dim currentContext As HttpContext = HttpContext.Current
    If Not (currentContext Is Nothing) Then
      Return currentContext.Request.RawUrl
    Else
      Throw New Exception("HttpContext.Current is Invalid")
    End If
  Catch e As Exception
    Throw New NotSupportedException("This provider requires a valid context.", e)
  End Try
End Function 'FindCurrentUrl

Remarks

Classes that derive from the SiteMapProvider class must implement the abstract FindSiteMapNode method.

The URL provided can be a virtual or absolute URL. It might also be a URL that uses application-relative syntax, such as ~/apprelativedirectory. Ensure that any implementation of the FindSiteMapNode method parse and handle application-relative syntax properly.

The XmlSiteMapProvider class, which is the default site map provider for ASP.NET, uses the URL of a SiteMapNode object as a key in the various collections that the classes maintain. Therefore, if a SiteMapNode provides a URL, it must be unique within the scope of the site map provider. If no URL is provided, a unique identifier is generated to identify the SiteMapNode.

Notes to Implementers

When overriding the FindSiteMapNode(String) method in a derived class, be sure to extend the search to any child providers, if a SiteMapNode object that matches the URL is not found by the provider in the current site map and the provider supports child providers.

See also

Applies to

FindSiteMapNode(HttpContext)

Retrieves a SiteMapNode object that represents the currently requested page using the specified HttpContext object.

public:
 virtual System::Web::SiteMapNode ^ FindSiteMapNode(System::Web::HttpContext ^ context);
public virtual System.Web.SiteMapNode FindSiteMapNode (System.Web.HttpContext context);
abstract member FindSiteMapNode : System.Web.HttpContext -> System.Web.SiteMapNode
override this.FindSiteMapNode : System.Web.HttpContext -> System.Web.SiteMapNode
Public Overridable Function FindSiteMapNode (context As HttpContext) As SiteMapNode

Parameters

context
HttpContext

The HttpContext used to match node information with the URL of the requested page.

Returns

A SiteMapNode that represents the currently requested page; otherwise, null, if no corresponding SiteMapNode can be found in the SiteMapNode or if the page context is null.

Remarks

The FindSiteMapNode method calls the abstract FindSiteMapNode method to retrieve a SiteMapNode object for the currently requested page based on the raw URL or the virtual path of the request. If no corresponding SiteMapNode is found in the SiteMap, null is returned.

The FindSiteMapNode method does not check whether a SiteMapNode is accessible to a user, by default.

See also

Applies to