Assuming the following url:
Dim nvc As NameValueCollection = HttpUtility.ParseQueryString(http://localhost/Index.aspx?A=23&A=7&A=1)
How can I remove a specific instance of A
from a NameValueCollection?
I can add an additional entry
nvc.Add("A", 10)
But can only seem to remove all instances
nvc.Remove("A")
I would prefer not to use string hacks.
Try out this method, string manipulation is not abused (I think).
The method extracts the Query part, selects the values of the Key specified except the values to remove, then rebuilds the Query using the UriBuilder.Query property (which can be set), finally returning a new formed Uri
, missing the removed Key-Value pairs.
Dim key As String = "A"
Dim valuesToRemove As String() = {"23", "1"}
Dim loc = New Uri("http://localhost/Index.aspx?A=23&A=7&A=1&B=2&B=4&C=23")
Dim newUri As Uri = UriRemoveKeyValues(loc, key, valuesToRemove)
Imports System.Web
Private Function UriRemoveKeyValues(uri As Uri, Key As String, Values As String()) As Uri
Dim nvc = HttpUtility.ParseQueryString(uri.Query)
Dim keyValues = nvc.GetValues(Key).Except(Values).ToList()
nvc.Remove(Key)
keyValues.ForEach(Sub(s) nvc.Add(Key, s))
Dim builder As New UriBuilder(uri) With {
.Query = nvc.ToString()
}
Return builder.Uri
End Function
You could also just get the Uri.Query
, split and rebuild the same way.
More string manipulations, though.