SiteMap.SiteMapResolve 事件

定義

當存取 CurrentNode 屬性時發生。

public:
 static event System::Web::SiteMapResolveEventHandler ^ SiteMapResolve;
public static event System.Web.SiteMapResolveEventHandler SiteMapResolve;
member this.SiteMapResolve : System.Web.SiteMapResolveEventHandler 
Public Shared Custom Event SiteMapResolve As SiteMapResolveEventHandler 

事件類型

範例

下列程式碼範例示範如何處理 SiteMapResolve ASP.NET 網頁上的事件,以修改網站導覽控制項所顯示的目標 URL,例如 SiteMapPath 控制項。 在此範例中,目前的頁面是線上佈告欄或論壇中的文章頁面。 為了轉譯更有意義的網站導覽,導覽控制項所顯示之節點的 URL 會附加內容相關的查詢字串。

注意

ASP.NET 網站導覽基礎結構會防範無限遞迴,以提供保護,並將與從 類別內 SiteMapResolveEventHandler 存取 CurrentNode 屬性相關聯的安全性風險降到最低。

下列程式碼屬於 Global.asax 檔案。 事件處理常式只會針對應用程式附加一次。 程式碼會辨識頁面是否實作 ISiteMapResolver 介面。 如果實作 介面,則會呼叫 函 ExpandForumPaths 式。

private void Page_Load(object sender, EventArgs e)
{
    // The ExpandForumPaths method is called to handle
    // the SiteMapResolve event.
    SiteMap.SiteMapResolve +=
      new SiteMapResolveEventHandler(this.ExpandForumPaths);
}

private SiteMapNode ExpandForumPaths(Object sender, SiteMapResolveEventArgs e)
{
    // The current node represents a Post page in a bulletin board forum.
    // Clone the current node and all of its relevant parents. This
    // returns a site map node that a developer can then
    // walk, modifying each node.Url property in turn.
    // Since the cloned nodes are separate from the underlying
    // site navigation structure, the fixups that are made do not
    // effect the overall site navigation structure.
    SiteMapNode currentNode = SiteMap.CurrentNode.Clone(true);
    SiteMapNode tempNode = currentNode;

    // Obtain the recent IDs.
    int forumGroupID = GetMostRecentForumGroupID();
    int forumID = GetMostRecentForumID(forumGroupID);
    int postID = GetMostRecentPostID(forumID);

    // The current node, and its parents, can be modified to include
    // dynamic querystring information relevant to the currently
    // executing request.
    if (0 != postID)
    {
        tempNode.Url = tempNode.Url + "?PostID=" + postID.ToString();
    }

    if ((null != (tempNode = tempNode.ParentNode)) &&
        (0 != forumID))
    {
        tempNode.Url = tempNode.Url + "?ForumID=" + forumID.ToString();
    }

    if ((null != (tempNode = tempNode.ParentNode)) &&
        (0 != forumGroupID))
    {
        tempNode.Url = tempNode.Url + "?ForumGroupID=" + forumGroupID.ToString();
    }

    return currentNode;
}
Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

    ' The ExpandForumPaths method is called to handle
    ' the SiteMapResolve event.
    AddHandler SiteMap.SiteMapResolve, AddressOf Me.ExpandForumPaths

End Sub

Private Function ExpandForumPaths(ByVal sender As Object, ByVal e As SiteMapResolveEventArgs) As SiteMapNode
    ' The current node represents a Post page in a bulletin board forum.
    ' Clone the current node and all of its relevant parents. This
    ' returns a site map node that a developer can then
    ' walk, modifying each node.Url property in turn.
    ' Since the cloned nodes are separate from the underlying
    ' site navigation structure, the fixups that are made do not
    ' effect the overall site navigation structure.
    Dim currentNode As SiteMapNode = SiteMap.CurrentNode.Clone(True)
    Dim tempNode As SiteMapNode = currentNode

    ' Obtain the recent IDs.
    Dim forumGroupID As Integer = GetMostRecentForumGroupID()
    Dim forumID As Integer = GetMostRecentForumID(forumGroupID)
    Dim postID As Integer = GetMostRecentPostID(forumID)

    ' The current node, and its parents, can be modified to include
    ' dynamic querystring information relevant to the currently
    ' executing request.
    If Not (0 = postID) Then
        tempNode.Url = tempNode.Url & "?PostID=" & postID.ToString()
    End If

    tempNode = tempNode.ParentNode
    If Not (0 = forumID) And Not (tempNode Is Nothing) Then
        tempNode.Url = tempNode.Url & "?ForumID=" & forumID.ToString()
    End If

    tempNode = tempNode.ParentNode
    If Not (0 = ForumGroupID) And Not (tempNode Is Nothing) Then
        tempNode.Url = tempNode.Url & "?ForumGroupID=" & forumGroupID.ToString()
    End If

    Return currentNode

End Function

下列程式碼會定義個別的介面。 (在網站專案中,您可以將此程式碼放在 App_Code folder.) 介面 ISiteMapResolverExpandForumPaths 定義 方法。

// These methods are just placeholders for the example.
// One option is to use the HttpContext or e.Context object
// to obtain the ID.
private int GetMostRecentForumGroupID()
{
    return 24;
}

private int GetMostRecentForumID(int forumGroupId)
{
    return 128;
}

private int GetMostRecentPostID(int forumId)
{
    return 317424;
}
' These methods are just placeholders for the example.
' One option is to use the HttpContext or e.Context object
' to obtain the ID.
Private Function GetMostRecentForumGroupID() As Integer
    Return 24
End Function

Private Function GetMostRecentForumID(ByVal forumGroupId As Integer) As Integer
    Return 128
End Function

Private Function GetMostRecentPostID(ByVal forumId As Integer) As Integer
    Return 317424
End Function

下列程式碼屬於至少位於網站地圖結構中三個節點的頁面。 頁面會實作 ISiteMapResolver 介面,以呼叫 ExpandForumPaths 方法。

<asp:SiteMapPath
id="SiteMapPath1"
runat="server"
RenderCurrentNodeAsLink="true" />
<asp:SiteMapPath
id="SiteMapPath1"
runat="server"
RenderCurrentNodeAsLink="true" />

備註

訂閱者會將 SiteMapResolveEventHandler 物件附加至靜態 SiteMapResolve 事件,以在存取 屬性時 CurrentNode 接收通知。 這可讓使用者在建立 SiteMapNode 目前執行頁面的標記法時實作自訂邏輯,而不需要自訂提供者實作。

如果您訂閱 SiteMapResolve 事件,您也會訂閱 SiteMapResolve 預設網站地圖提供者上的事件。

適用於

另請參閱