2016-08-15 87 views
1

當頁面顯示時,我需要隱藏查詢字符串。 試了幾個例子,應該工作,但他們不適合我。 基於母版頁(不知道這有什麼區別)如何在頁面填充後隱藏查詢字符串

嘗試#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

你究竟在做什麼,爲什麼?修改* request *的查詢字符串聽起來不對,因爲您爲什麼需要這樣做?只是修改* response *的查詢字符串是沒有意義的,因爲沒有一個。 – David

+0

請只包含您的問題的具體代碼,並更詳細地解釋您正在嘗試解決的問題,例如現在正在發生的事件以及您希望發生的事件的順序。 –

+0

您可以使用重定向來隱藏查詢字符串。即將查詢參數放入會話中,並將請求發送到處理請求的新頁面。這往往有一個副作用,使您的應用程序有狀態,這是不可取的 – Tibrogargan

回答

0

我可以想到一些選擇,但沒有一個是「容易」做的,但都可能有所幫助。

1 - 使用JavaScript加載頁面以編輯URL。雖然URL可能暫時可用,但客戶端腳本可能會更改此值 - 有關更多信息,請參閱Change the URL in the browser without loading the new page using JavaScript

2 - 讓用戶通過中間頁面訪問應用程序,該頁面存儲您需要的數據,然後將用戶(通過Response.Redirect)重定向到您熟悉的URL。存儲您需要通過Cookie,HTML本地存儲或會話訪問的數據,並在用戶登錄的最終頁面重新訪問該數據。

3 - 將數據保存在URL中,但使用加密來保護您發現敏感或擔心操作的數據。

之所以這麼說,你正在試圖解決的問題只是想保護的QueryString數據,這本質上是不安全。我對你的應用程序一無所知,但如果你擔心數據的敏感性,你可以繼續把口紅放在豬身上..但它仍然是一隻豬(又名..當它不是使用查詢字符串你想保護的東西)。

+0

謝謝你,ewitkows,我也在javascript中嘗試過windows.location,它對我來說是失敗的。我有紅色的討論你建議,它主要是從一個頁面重定向到另一個,我試圖重定向到它自我,但是,我需要保持頁面填充原始請求中傳入的任何值。 – user2138121

相關問題