當SiteMap碰到QueryString

當SiteMap碰到QueryString

相信很多人都有用過SiteMapPath來當作網站的巡迴導覽,

只要在網頁拖曳SiteMapPath的控制項,然後在Web.sitemap裡設定xxx.aspx即可發揮效用

可是當我們的網頁有一長串的QueryString,不管有幾個,只要沒有設定在Web.sitemap裡

此時我們的SiteMapPath就出不來了

為了要解決這問題,我們只要去覆寫FindSiteMapNode這個method就可以了

我們宣告一個Class名稱為SiteMapProvider

在一開頭前面,我們去繼承XmlSiteMapProvider,醬子我們才可以覆寫FindSiteMapNode Method

接著我們在程式裡撰寫

 


Public Overrides Function FindSiteMapNode(ByVal rawUrl As String) As SiteMapNode
      
End Function

我們要做的處理就是寫在這裡面,rawUrl是傳進來的Url

詳細的程式內容如下

 


Imports Microsoft.VisualBasic
Imports System.Web

Public Class SiteMapProvider
    Inherits XmlSiteMapProvider

    Public Overrides Function FindSiteMapNode(ByVal rawUrl As String) As SiteMapNode
        Dim smn As SiteMapNode = MyBase.FindSiteMapNode(rawUrl)
        If (smn Is Nothing) Then
            '拆解aspx跟QueryStriing
            Dim arrUrl() As String = rawUrl.Split("?")
            Dim sPage As String = arrUrl(0)
            Dim sQuery As String = String.Empty

            If (arrUrl.Length >= 2) Then
                sQuery = arrUrl(1)
            End If

            Dim aQuery() As String = sQuery.Split("&") '將QueryString拆解
            For i As Integer = 1 To aQuery.Length - 1
                '利用網頁網址與拆解的QueryString去尋找節點
                smn = MyBase.FindSiteMapNode(sPage + "?" + aQuery(i))
                If (smn IsNot Nothing) Then
                    Exit For
                End If
            Next

            If (smn Is Nothing) Then
                smn = MyBase.FindSiteMapNode(sPage)
            End If

        End If
        Return smn

    End Function
End Class

完成後,不管你有幾個QueryString,我們SiteMapPath都出的來。大概如下圖的呈現

Course

WebSite1.rar


reference SiteMap問題解決