使用Response.Redirect和SiteMaps时出现奇怪的行为

时间:2011-10-07 10:10:49

标签: .net asp.net vb.net

我的ASP.NET(VB.NET)应用程序使用SiteMaps在每个页面的顶部显示导航菜单。在某些页面的代码中,我正在动态修改SiteMaps节点的URL(将参数添加到URL的末尾),例如说我有some.aspx.vb和anotherPage.aspx.vb,它们都包含以下内容:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    AddHandler SiteMap.SiteMapResolve, AddressOf Me.ExpandForumPaths

    If Not Page.IsPostBack Then
        Setup()
    End If

End Sub

Private Function ExpandForumPaths(ByVal sender As Object, ByVal e As SiteMapResolveEventArgs) As SiteMapNode
    Dim currentNode As SiteMapNode = Nothing

    Try
        currentNode = SiteMap.CurrentNode.Clone(True)
        ' Do some stuff

    Catch ex As Exception
        ' Do nothing - don't want app falling over because of issue with nav menu
    End Try

    Return currentNode
End Function

我的问题是这个,如果我在somePage.aspx背后的代码中,我做了一个Response.Redirect(“〜/ anotherPage.aspx”) - 然后,当加载anotherPage.aspx.vb时,它是somePage.aspx.vb的ExpandFormumPaths方法被命中,而不是anotherPage.aspx.vb。

我的理解是Response.Redirect告诉浏览器对提供的URL做一个新的请求 - 我不知道为什么它会触及属于上一页的方法。

我已经尝试修改方法名称(即在所有类中没有名为ExpandFormumPaths的节点处理方法),但我仍遇到同样的问题。

当我直接访问anotherPage.aspx的URL时,会遇到正确的方法,只有在我使用Response.Redirect时才会发生这种情况。

有什么想法吗?

3 个答案:

答案 0 :(得分:0)

您是否尝试过Response.Redirect("~/anotherPage.aspx", true)true将结束当前页面的处理,听起来就是这样。

答案 1 :(得分:0)

请检查anotherPage.aspx中@Page指令中的CodeBehind文件。它应该是“anotherPage.aspx.vb”,看起来就是指“somePage.aspx.vb”。

答案 2 :(得分:0)

按顺序排序 - 以防万一其他人有同样的问题。我把它包含在文件后面的相同代码中。

' Remove the site map resolver on unload. Causes all kinds of weird issues if we don't do this.
Private Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
    RemoveHandler SiteMap.SiteMapResolve, AddressOf Me.ExpandForumPaths

End Sub
相关问题