VB.NET || How To Add, Update & Remove Values From A URL Query String Using VB.NET

The following is a module with functions which demonstrates how to add, update and remove parameters from a query string using VB.NET.
The following functions use System.Web to modify the url.
Note: To use the functions in this module, make sure you have a reference to ‘System.Web‘ in your project.
One way to do this is, in your Solution Explorer (where all the files are shown with your project), right click the ‘References‘ folder, click ‘Add Reference‘, then type ‘System.Web‘ in the search box, and add the reference titled System.Web in the results Tab.
1. Add & Update – Single Parameter
The example below demonstrates the use of ‘Utils.Http.AddParameter‘ to add a query string parameter to a url.
This function will add the parameter if it does not exists. Or it will update the parameter if it does.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
' Add & Update - Single Parameter ' Declare url Dim url = "http://www.programmingnotes.org/" ' Add single parameter to the url Dim result = Utils.Http.AddParameter(url, New KeyValuePair(Of String, String)("ids", "1987")) ' Display result Debug.Print(result) ' expected output: ' http://www.programmingnotes.org/?ids=1987 |
2. Add & Update – Multiple Parameters
The example below demonstrates the use of ‘Utils.Http.AddParameter‘ to add a query string parameter to a url.
This function will add the parameter if it does not exists. Or it will update the parameter if it does.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
' Add & Update - Multiple Parameters ' Declare url Dim url = "http://www.programmingnotes.org/" ' Add multiple parameters to the url Dim addition = New Specialized.NameValueCollection From { {"ids", "1987"}, {"ids", "1991"}, {"names", "Kenneth"}, {"names", "Jennifer"} } ' Update the url Dim result = Utils.Http.AddParameter(url, addition) ' Display result Debug.Print(result) ' expected output: ' http://www.programmingnotes.org/?ids=1987%2c1991&names=Kenneth%2cJennifer |
3. Remove – Single Parameter
The example below demonstrates the use of ‘Utils.Http.RemoveParameter‘ to remove a query string parameter from a url.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
' Remove - Single Parameter ' Declare url Dim url = "http://www.programmingnotes.org/?ids=1987,1991&names=Kenneth,Jennifer" ' Remove single parameter from the url Dim result = Utils.Http.RemoveParameter(url, "names") ' Display result Debug.Print(result) ' expected output: ' http://www.programmingnotes.org/?ids=1987%2c1991 |
4. Remove – Multiple Parameters
The example below demonstrates the use of ‘Utils.Http.RemoveParameter‘ to remove multiple query string parameters from a url.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
' Remove - Multiple Parameters ' Declare url Dim url = "http://www.programmingnotes.org/?ids=1987,1991&names=Kenneth,Jennifer" ' Remove multiple parameters from the url Dim result = Utils.Http.RemoveParameter(url, New String() {"ids", "names"}) ' Display result Debug.Print(result) ' expected output: ' http://www.programmingnotes.org/ |
5. Clear – Remove All Parameters
The example below demonstrates the use of ‘Utils.Http.ClearParameters‘ to remove all query string parameters from a url.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
' Clear - Remove All Parameters ' Declare url Dim url = "http://www.programmingnotes.org/?ids=1987,1991&names=Kenneth,Jennifer" ' Remove all parameters from the url Dim result = Utils.Http.ClearParameters(url) ' Display result Debug.Print(result) ' expected output: ' http://www.programmingnotes.org/ |
6. Utils Namespace
The following is the Utils Namespace. Include this in your project to start using!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
' ============================================================================ ' Author: Kenneth Perkins ' Taken From: http://programmingnotes.org/ ' Date: Nov 17, 2020 ' File: Utils.vb ' Description: Handles general utility functions ' ============================================================================ Option Strict On Option Explicit On Namespace Global.Utils Namespace Http Public Module modHttp ''' <summary> ''' Adds or updates the query string parameters to a url ''' </summary> ''' <param name="url">The url to append parameters</param> ''' <param name="query">The parameters to add to the url</param> ''' <returns>The url appended with the specified query</returns> Public Function AddParameter(url As String, query As Specialized.NameValueCollection) As String Return UpdateQuery(url, Sub(queryString) For Each key In query.AllKeys queryString(key) = query(key) Next End Sub) End Function ''' <summary> ''' Adds or updates the query string parameters to a url ''' </summary> ''' <param name="url">The url to append parameters</param> ''' <param name="param">The parameters to add to the url</param> ''' <returns>The url appended with the specified query</returns> Public Function AddParameter(url As String, param As KeyValuePair(Of String, String)) As String Return AddParameter(url, New Specialized.NameValueCollection From {{param.Key, param.Value}}) End Function ''' <summary> ''' Removes the parameters with the matching keys from the query string of a url ''' </summary> ''' <param name="url">The url to remove parameters</param> ''' <param name="keys">The parameter keys to remove from the url</param> ''' <returns>The url with the parameters removed</returns> Public Function RemoveParameter(url As String, keys As IEnumerable(Of String)) As String Return UpdateQuery(url, Sub(queryString) For Each key In keys If queryString.AllKeys.Contains(key) Then queryString.Remove(key) End If Next End Sub) End Function ''' <summary> ''' Removes the parameter with the matching key from the query string of a url ''' </summary> ''' <param name="url">The url to remove a parameter</param> ''' <param name="key">The parameter key to remove from the url</param> ''' <returns>The url with the parameter removed</returns> Public Function RemoveParameter(url As String, key As String) As String Return RemoveParameter(url, {key}) End Function ''' <summary> ''' Removes all parameters from the query string of a url ''' </summary> ''' <param name="url">The url to remove parameters</param> ''' <returns>The url with the parameters removed</returns> Public Function ClearParameters(url As String) As String Return UpdateQuery(url, Sub(queryString) For Each key In queryString.AllKeys queryString.Remove(key) Next End Sub) End Function Private Function UpdateQuery(url As String, modifyQuery As Action(Of Specialized.NameValueCollection)) As String Dim uriBuilder = New System.UriBuilder(url) Dim query = System.Web.HttpUtility.ParseQueryString(uriBuilder.Query) modifyQuery(query) uriBuilder.Query = query.ToString Return uriBuilder.Uri.ToString End Function End Module End Namespace End Namespace ' http://programmingnotes.org/ |
7. More Examples
Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
' ============================================================================ ' Author: Kenneth Perkins ' Taken From: http://programmingnotes.org/ ' Date: Nov 17, 2020 ' File: Program.vb ' Description: The following demonstrates the use of the Utils Namespace. ' ============================================================================ Option Strict On Option Explicit On Imports System Public Module Program Sub Main(args As String()) Try ' Declare url Dim url = "http://www.programmingnotes.org/" ' Add single key value parameter to the url Dim result = Utils.Http.AddParameter(url, New KeyValuePair(Of String, String)("ids", "1987")) ' Display result Display(result) Display("") ' Declare url Dim url2 = "http://www.programmingnotes.org/" ' Add multiple key value parameters to the url Dim addition = New Specialized.NameValueCollection From { {"ids", "1987"}, {"ids", "1991"}, {"names", "Kenneth"}, {"names", "Jennifer"} } ' Update the url Dim result2 = Utils.Http.AddParameter(url2, addition) ' Display result Display(result2) Display("") ' Declare url Dim url3 = "http://www.programmingnotes.org/?ids=1987,1991&names=Kenneth,Jennifer" ' Remove single parameter from the url Dim result3 = Utils.Http.RemoveParameter(url3, "names") ' Display result Display(result3) Display("") ' Declare url Dim url4 = "http://www.programmingnotes.org/?ids=1987,1991&names=Kenneth,Jennifer" ' Remove multiple parameters from the url Dim result4 = Utils.Http.RemoveParameter(url4, New String() {"ids", "names"}) ' Display result Display(result4) Display("") ' Declare url Dim url5 = "http://www.programmingnotes.org/?ids=1987,1991&names=Kenneth,Jennifer" ' Remove multiple parameters from the url Dim result5 = Utils.Http.ClearParameters(url5) ' Display result Display(result5) Catch ex As Exception Display(ex.ToString) Finally Console.ReadLine() End Try End Sub Public Sub Display(message As String) Console.WriteLine(message) Debug.Print(message) End Sub End Module ' http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
Leave a Reply