I need to hide Query string when page displayed. Tried a few examples, that supposed to work, however they are not work for me. My client page based on Master page(not sure if it make any difference)
Attempt #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
Attempt #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
Any advise about what am I doing wrong greatly appreciated.