自定义SiteMap后,SiteMap.SiteMapResolve事件处理程序不执行解决办法
我们在建设asp.net网站时,有时不可避免的会用到多个SiteMap配置文档
在MSDN中有说明,只需要要web.config中添加sitemap配置节,并在sitemappath控件中指定sitemapprovider就可以实现多个sitemap文档的应用
例如:
代码
<siteMap enabled="true" defaultProvider="sitemap">
<providers>
<clear />
<add name="sitemap" siteMapFile="web.sitemap" type="System.Web.XmlSiteMapProvider" description="XmlSiteMapProvider that loads nttc.sitemap"/>
<add name="nttc" siteMapFile="nttc.sitemap" type="System.Web.XmlSiteMapProvider" description="XmlSiteMapProvider that loads nttc.sitemap"/>
</providers>
</siteMap>
<providers>
<clear />
<add name="sitemap" siteMapFile="web.sitemap" type="System.Web.XmlSiteMapProvider" description="XmlSiteMapProvider that loads nttc.sitemap"/>
<add name="nttc" siteMapFile="nttc.sitemap" type="System.Web.XmlSiteMapProvider" description="XmlSiteMapProvider that loads nttc.sitemap"/>
</providers>
</siteMap>
在相关页面添加
<asp:SiteMapPath ID="SiteMapPath1" runat="server" SiteMapProvider="nttc"></asp:SiteMapPath>
如果需要通过编程方式修改,则在相关的页面中添加事件处理程序(在默认的情况)
代码
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
AddHandler System.Web.SiteMap.SiteMapResolve, AddressOf SiteMapResolve
End Sub
Protected Function SiteMapResolve(ByVal sender As Object, ByVal e As SiteMapResolveEventArgs) As SiteMapNode
Dim c As SiteMapNode = e.Provider.CurrentNode.Clone()
c.Title = "测试"
Return c
End Function
Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
RemoveHandler SiteMap.SiteMapResolve, AddressOf SiteMapResolve
End Sub
AddHandler System.Web.SiteMap.SiteMapResolve, AddressOf SiteMapResolve
End Sub
Protected Function SiteMapResolve(ByVal sender As Object, ByVal e As SiteMapResolveEventArgs) As SiteMapNode
Dim c As SiteMapNode = e.Provider.CurrentNode.Clone()
c.Title = "测试"
Return c
End Function
Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
RemoveHandler SiteMap.SiteMapResolve, AddressOf SiteMapResolve
End Sub
但在多个SiteMap文档配置的情况下,执行页面结果能显示当前节点在nttc.sitemap的配置信息,即已找到页面但无法通过绑定的SiteMapResolve事件处理程序来修改,也就是添加的SiteMapResolve事件处理程序此时并未执行,如果将nttc在web.config设置为defaultProvider的情况下SiteMapResolve事件处理程序被执行。
SiteMap.SiteMapResolve应该是用于默认的Provider的事件,经过测试后,通过绑定到当前的Provider.SiteMapResolve来实现
将原来的
AddHandler System.Web.SiteMap.SiteMapResolve, AddressOf SiteMapResolve
修改为:
Dim provider As SiteMapProvider = SiteMapPath1.Provider
AddHandler provider .SiteMapResolve, AddressOf SiteMapResolve
AddHandler provider .SiteMapResolve, AddressOf SiteMapResolve
测试后成功