SiteMapProvider.FindSiteMapNode 方法

定義

在衍生類別中覆寫時,擷取表示網頁的 SiteMapNode 物件。

多載

FindSiteMapNode(String)

在衍生類別中覆寫時,擷取表示位於指定 URL 之網頁的 SiteMapNode 物件。

FindSiteMapNode(HttpContext)

使用指定的 SiteMapNode 物件,擷取表示目前所要求之網頁的 HttpContext 物件。

FindSiteMapNode(String)

在衍生類別中覆寫時,擷取表示位於指定 URL 之網頁的 SiteMapNode 物件。

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

參數

rawUrl
String

URL,其可識別要擷取 SiteMapNode 的頁面。

傳回

表示 rawURL 所識別之網頁的 SiteMapNode,但如果找不到對應的 SiteMapNode,或安全性調整已啟用,而無法傳回目前使用者的 SiteMapNode,則為 null

範例

下列程式碼範例示範如何在 FindSiteMapNode 實作抽象 SiteMapProvider 類的類別中實作 方法。 會 SimpleTextSiteMapProvider 使用名為 FindUrl 的協助程式方法,從 物件取得目前顯示頁面的 HttpContext URL。

此程式碼範例是提供給 類別之較大範例的 SiteMapProvider 一部分。

// 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

備註

衍生自 類別的 SiteMapProvider 類別必須實作抽象 FindSiteMapNode 方法。

提供的 URL 可以是虛擬或絕對 URL。 它也可能是使用應用程式相對語法的 URL,例如 ~/apprelativedirectory 。 請確定方法的任何實作 FindSiteMapNode 都正確剖析及處理應用程式相對語法。

類別 XmlSiteMapProvider 是 ASP.NET 的預設網站地圖提供者,會使用 物件的 URL SiteMapNode 作為類別所維護之各種集合中的索引鍵。 因此,如果 SiteMapNode 提供 URL,它必須在網站地圖提供者的範圍內是唯一的。 如果未提供 URL,則會產生唯一識別碼來識別 SiteMapNode

給實施者的注意事項

FindSiteMapNode(String) 衍生類別中覆寫 方法時,如果 SiteMapNode 目前網站地圖中的提供者找不到符合 URL 的物件,而且提供者支援子提供者,請務必將搜尋延伸至任何子提供者。

另請參閱

適用於

FindSiteMapNode(HttpContext)

使用指定的 SiteMapNode 物件,擷取表示目前所要求之網頁的 HttpContext 物件。

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

參數

context
HttpContext

HttpContext,用來使節點資訊與所要求之網頁的 URL 相對應。

傳回

表示目前所要求之網頁的 SiteMapNode,如果 SiteMapNode 中找不到對應的 SiteMapNode,或網頁內容為 null,則為 null

備註

方法 FindSiteMapNode 會呼叫抽象 FindSiteMapNode 方法,根據原始 URL 或要求的虛擬路徑,擷取 SiteMapNode 目前要求頁面的物件。 如果在 中 SiteMap 找不到對應的 SiteMapNodenull 則會傳回 。

根據預設,此方法 FindSiteMapNode 不會檢查 使用者是否 SiteMapNode 可存取 。

另請參閱

適用於