Posted By

DaveChild on 09/11/08


Tagged

querystring


Versions (?)

VB.NET Add and Remove Querystring Variables


 / Published in: VB.NET
 

  1. Public Function AddQuerystringVar(ByVal strURL As String, ByVal QuerystringKey As String, ByVal QuerystringValue As String) As String
  2. strURL = RemoveQuerystringVar(strURL, QuerystringKey) ' Remove value if already in URL
  3. If InStr(strURL, "?") = 0 Then
  4. Return strURL + "?" + QuerystringKey + "=" + QuerystringValue
  5. Else
  6. Return strURL + "&" + QuerystringKey + "=" + QuerystringValue
  7. End If
  8. End Function
  9.  
  10. Public Function RemoveQuerystringVar(ByVal strURL As String, ByVal QuerystringKey As String) As String
  11. Dim re As New Regex("(.*)(\?|&)" + QuerystringKey + "=[^&]+?(&)(.*)", RegexOptions.IgnoreCase)
  12. strURL = re.Replace(strURL + "&", "$1$2$4")
  13. strURL = Mid(strURL, 1, (strURL.Length - 1))
  14. Return strURL
  15. End Function

Report this snippet  

You need to login to post a comment.