如何在页面填充后隐藏查询字符串

时间:2016-08-15 19:24:18

标签: asp.net vb.net

我需要在显示页面时隐藏查询字符串。 尝试了几个应该工作的例子,但它们对我不起作用。 我的客户页面基于母版页(不确定是否有任何区别)

尝试#1

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim isreadonly As PropertyInfo = GetType(System.Collections.Specialized.NameValueCollection).GetProperty("IsReadOnly", BindingFlags.Instance Or BindingFlags.NonPublic)
    ' make collection editable
    isreadonly.SetValue(Me.Request.QueryString, False, Nothing)
    Me.Request.QueryString.Clear()

End Sub    

尝试#2

Private Function BuildQueryString(ByVal useKeyValues As NameValueCollection, ByVal RemoveKeys As List(Of String)) As String
    Dim Key As String
    Dim retQueryString As String = ""
    Dim AddKeyValue As String

    'Step through each key value pair
    For Each Key In useKeyValues.AllKeys

        'if key isn't in the RemoveKeys list then add to the new querystring
        If Not RemoveKeys.Contains(Key) Then
            AddKeyValue = Key + "=" + useKeyValues(Key)

            If String.IsNullOrEmpty(retQueryString) Then
                retQueryString += "?" + AddKeyValue
            Else
                retQueryString += "&" + AddKeyValue
            End If
        End If
    Next

    Return retQueryString
End Function

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


    Dim SearchValues As NameValueCollection
    Dim KeysToRemove As New List(Of String)

    Try
        SearchValues = Request.QueryString

        'figure out what keys need to be removed and add one or more
        KeysToRemove.Add("OrderId")

        If SearchValues.Count > 0 Then
            'writes original url with querystring
            Response.Write(Request.RawUrl + "<br><Br>")

            'writes new url with new querystring

            Response.Write(Request.Url.AbsolutePath + BuildQueryString(SearchValues, KeysToRemove))
        Else
            'For example redirects to this page adding querystring
            Response.Redirect("NewPage1a.aspx?SomeKey=SomeValue&SomeOtherKey=AnotherValue&OrderId=2")
        End If

    Catch ex As Exception

    End Try

End Sub

任何关于我做错事的建议都非常感激。

0 个答案:

没有答案