語言

SiteMapProvider.FindSiteMapNode 方法

定義

當在衍生類別中覆寫時,會取得 SiteMapNode 代表頁面的物件。

多載

名稱 Description
FindSiteMapNode(String)

當在派生類別中覆寫時,會取得 SiteMapNode 一個代表指定網址頁面的物件。

FindSiteMapNode(HttpContext)

利用指定的SiteMapNode物件擷取HttpContext代表當前請求頁面的物件。

FindSiteMapNode(String)

當在派生類別中覆寫時,會取得 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

一個用來識別要取 SiteMapNode回 .

傳回

SiteMapNode A 代表由 識別的rawURL頁面;否則,null若未找到對應SiteMapNode頁面或啟用安全修剪且SiteMapNode無法回傳給當前使用者,則 。

範例

以下程式碼範例示範如何在實作抽象FindSiteMapNode類別的類別中實作該SiteMapProvider方法。 它 SimpleTextSiteMapProvider 使用一個名為 FindUrl的輔助方法,從物件中取得目前顯示頁面 HttpContext 的網址。

此程式碼範例是本類別更大範例 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,例如 ~/apprelativedirectory。 確保任何方法 FindSiteMapNode 實作都能正確解析並處理應用相關語法。

XmlSiteMapProvider 類別是 ASP.NET 的預設網站地圖提供者,會將 SiteMapNode 物件的 URL 作為類別維護的各種集合中的鍵。 因此,若 a SiteMapNode 提供 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 用來將節點資訊與請求頁面的網址匹配。

傳回

SiteMapNode A 代表目前請求的頁面;否則,null若 中找不到SiteMapNode對應SiteMapNode頁面,或頁面上下文為 null。

備註

該 FindSiteMapNode 方法 FindSiteMapNode 呼叫抽象方法,根據原始網址或請求的虛擬路徑,為目前請求頁面擷取 SiteMapNode 物件。 若在中SiteMapNode找不到對應SiteMap的,則null回傳。

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

另請參閱

適用於