Return to Snippet

Revision: 8276
at September 11, 2008 10:01 by DaveChild


Initial Code
Public Function AddQuerystringVar(ByVal strURL As String, ByVal QuerystringKey As String, ByVal QuerystringValue As String) As String
    strURL = RemoveQuerystringVar(strURL, QuerystringKey) ' Remove value if already in URL
    If InStr(strURL, "?") = 0 Then
        Return strURL + "?" + QuerystringKey + "=" + QuerystringValue
    Else
        Return strURL + "&" + QuerystringKey + "=" + QuerystringValue
    End If
End Function

Public Function RemoveQuerystringVar(ByVal strURL As String, ByVal QuerystringKey As String) As String
    Dim re As New Regex("(.*)(\?|&)" + QuerystringKey + "=[^&]+?(&)(.*)", RegexOptions.IgnoreCase)
    strURL = re.Replace(strURL + "&", "$1$2$4")
    strURL = Mid(strURL, 1, (strURL.Length - 1))
    Return strURL
End Function

Initial URL

                                

Initial Description

                                

Initial Title
VB.NET Add and Remove Querystring Variables

Initial Tags

                                

Initial Language
VB.NET