SiteMap.SiteMapResolve Kejadian
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Terjadi ketika CurrentNode properti diakses.
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
Jenis Acara
Contoh
Contoh kode berikut menunjukkan cara menangani SiteMapResolve peristiwa di halaman web ASP.NET untuk mengubah URL target yang ditampilkan oleh kontrol navigasi situs, seperti SiteMapPath kontrol. Dalam contoh ini, halaman saat ini adalah halaman posting di papan buletin online atau forum. Untuk merender navigasi situs yang lebih bermakna, URL simpul yang ditampilkan oleh kontrol navigasi ditambahkan dengan string kueri yang relevan konteks.
Catatan
Infrastruktur navigasi situs ASP.NET melindungi dari rekursi tak terbatas, yang memberikan perlindungan dan meminimalkan risiko keamanan yang terkait dengan mengakses CurrentNode properti dari dalam SiteMapResolveEventHandler kelas.
Kode berikut termasuk dalam file Global.asax. Penanganan aktivitas hanya akan dilampirkan sekali untuk aplikasi. Kode mengenali apakah halaman mengimplementasikan ISiteMapResolver
antarmuka. Jika antarmuka diimplementasikan, ExpandForumPaths
fungsi dipanggil.
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
Kode berikut mendefinisikan antarmuka terpisah. (Dalam proyek situs Web, Anda dapat meletakkan kode ini di folder App_Code.) Antarmuka ISiteMapResolver
mendefinisikan ExpandForumPaths
metode .
// 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
Kode berikut termasuk dalam halaman yang setidaknya memiliki tiga simpul dalam struktur peta situs. Halaman mengimplementasikan ISiteMapResolver
antarmuka, yang memungkinkan metode dipanggil ExpandForumPaths
.
<asp:SiteMapPath
id="SiteMapPath1"
runat="server"
RenderCurrentNodeAsLink="true" />
<asp:SiteMapPath
id="SiteMapPath1"
runat="server"
RenderCurrentNodeAsLink="true" />
Keterangan
Pelanggan melampirkan SiteMapResolveEventHandler objek ke peristiwa statis SiteMapResolve untuk menerima pemberitahuan saat CurrentNode properti diakses. Ini memungkinkan pengguna untuk menerapkan logika kustom saat membuat SiteMapNode representasi halaman yang saat ini dijalankan tanpa memerlukan implementasi penyedia kustom.
Jika Anda berlangganan acara, SiteMapResolve Anda juga berlangganan SiteMapResolve peristiwa di penyedia peta situs default.