I would like to parse a string such as p1=6&p2=7&p3=8
into a NameValueCollection
.
What is the most elegant way of doing this when you don\'t have access to the Page.Request
object?
I would like to parse a string such as p1=6&p2=7&p3=8
into a NameValueCollection
.
What is the most elegant way of doing this when you don\'t have access to the Page.Request
object?
There\'s a built-in .NET utility for this: HttpUtility.ParseQueryString
// C#
NameValueCollection qscoll = HttpUtility.ParseQueryString(querystring);
\' VB.NET
Dim qscoll As NameValueCollection = HttpUtility.ParseQueryString(querystring)
You may need to replace querystring
with new Uri(fullUrl).Query
.
HttpUtility.ParseQueryString will work as long as you are in a web app or don\'t mind including a dependency on System.Web. Another way to do this is:
NameValueCollection queryParameters = new NameValueCollection();
string[] querySegments = queryString.Split(\'&\');
foreach(string segment in querySegments)
{
string[] parts = segment.Split(\'=\');
if (parts.Length > 0)
{
string key = parts[0].Trim(new char[] { \'?\', \' \' });
string val = parts[1].Trim();
queryParameters.Add(key, val);
}
}
A lot of the answers are providing custom examples because of the accepted answer\'s dependency on System.Web. From the Microsoft.AspNet.WebApi.Client NuGet package there is a UriExtensions.ParseQueryString, method that can also be used:
var uri = new Uri(\"https://stackoverflow.com/a/22167748?p1=6&p2=7&p3=8\");
NameValueCollection query = uri.ParseQueryString();
So if you want to avoid the System.Web dependency and don\'t want to roll your own, this is a good option.
I wanted to remove the dependency on System.Web so that I could parse the query string of a ClickOnce deployment, while having the prerequisites limited to the \"Client-only Framework Subset\".
I liked rp\'s answer. I added some additional logic.
public static NameValueCollection ParseQueryString(string s)
{
NameValueCollection nvc = new NameValueCollection();
// remove anything other than query string from url
if(s.Contains(\"?\"))
{
s = s.Substring(s.IndexOf(\'?\') + 1);
}
foreach (string vp in Regex.Split(s, \"&\"))
{
string[] singlePair = Regex.Split(vp, \"=\");
if (singlePair.Length == 2)
{
nvc.Add(singlePair[0], singlePair[1]);
}
else
{
// only one key with no value specified in query string
nvc.Add(singlePair[0], string.Empty);
}
}
return nvc;
}
I needed a function that is a little more versatile than what was provided already when working with OLSC queries.
Here is my solution:
Public Shared Function ParseQueryString(ByVal uri As Uri) As System.Collections.Specialized.NameValueCollection
Dim result = New System.Collections.Specialized.NameValueCollection(4)
Dim query = uri.Query
If Not String.IsNullOrEmpty(query) Then
Dim pairs = query.Substring(1).Split(\"&\"c)
For Each pair In pairs
Dim parts = pair.Split({\"=\"c}, 2)
Dim name = System.Uri.UnescapeDataString(parts(0))
Dim value = If(parts.Length = 1, String.Empty,
System.Uri.UnescapeDataString(parts(1)))
result.Add(name, value)
Next
End If
Return result
End Function
It may not be a bad idea to tack <Extension()>
on that too to add the capability to Uri itself.
To do this without System.Web
, without writing it yourself, and without additional NuGet packages:
System.Net.Http.Formatting
using System.Net.Http;
Use this code:
new Uri(uri).ParseQueryString()
https://msdn.microsoft.com/en-us/library/system.net.http.uriextensions(v=vs.118).aspx
private void button1_Click( object sender, EventArgs e )
{
string s = @\"p1=6&p2=7&p3=8\";
NameValueCollection nvc = new NameValueCollection();
foreach ( string vp in Regex.Split( s, \"&\" ) )
{
string[] singlePair = Regex.Split( vp, \"=\" );
if ( singlePair.Length == 2 )
{
nvc.Add( singlePair[ 0 ], singlePair[ 1 ] );
}
}
}
I just realized that Web API Client has a ParseQueryString
extension method that works on a Uri
and returns a HttpValueCollection
:
var parameters = uri.ParseQueryString();
string foo = parameters[\"foo\"];
If you want to avoid the dependency on System.Web that is required to use HttpUtility.ParseQueryString, you could use the Uri
extension method ParseQueryString
found in System.Net.Http
.
Make sure to add a reference (if you haven\'t already) to System.Net.Http
in your project.
Note that you have to convert the response body to a valid Uri
so that ParseQueryString
(in System.Net.Http
)works.
string body = \"value1=randomvalue1&value2=randomValue2\";
// \"http://localhost/query?\" is added to the string \"body\" in order to create a valid Uri.
string urlBody = \"http://localhost/query?\" + body;
NameValueCollection coll = new Uri(urlBody).ParseQueryString();
Just access Request.QueryString. AllKeys mentioned as another answer just gets you an array of keys.
HttpUtility.ParseQueryString(Request.Url.Query)
return is HttpValueCollection
(internal class). It inherits from NameValueCollection
.
var qs = HttpUtility.ParseQueryString(Request.Url.Query);
qs.Remove(\"foo\");
string url = \"~/Default.aspx\";
if (qs.Count > 0)
url = url + \"?\" + qs.ToString();
Response.Redirect(url);
I just whipped this together from the source code of Mono. It contains the HttpUtility and all it\'s dependencies (like IHtmlString, Helpers, HttpEncoder, HttpQSCollection).
Then use HttpUtility.ParseQueryString
.
https://gist.github.com/bjorn-ali-goransson/b04a7c44808bb2de8cca3fc9a3762f9c
Since everyone seems to be pasting his solution.. here\'s mine :-)
I needed this from within a class library without System.Web
to fetch id parameters from stored hyperlinks.
Thought I\'d share because I find this solution faster and better looking.
public static class Statics
public static Dictionary<string, string> QueryParse(string url)
{
Dictionary<string, string> qDict = new Dictionary<string, string>();
foreach (string qPair in url.Substring(url.IndexOf(\'?\') + 1).Split(\'&\'))
{
string[] qVal = qPair.Split(\'=\');
qDict.Add(qVal[0], Uri.UnescapeDataString(qVal[1]));
}
return qDict;
}
public static string QueryGet(string url, string param)
{
var qDict = QueryParse(url);
return qDict[param];
}
}
Usage:
Statics.QueryGet(url, \"id\")
Hit up Request.QueryString.Keys for a NameValueCollection of all query string parameters.
To get all Querystring values try this:
Dim qscoll As NameValueCollection = HttpUtility.ParseQueryString(querystring)
Dim sb As New StringBuilder(\"<br />\")
For Each s As String In qscoll.AllKeys
Response.Write(s & \" - \" & qscoll(s) & \"<br />\")
Next s
var q = Request.QueryString;
NameValueCollection qscoll = HttpUtility.ParseQueryString(q.ToString());
I translate to C# version of josh-brown in VB
private System.Collections.Specialized.NameValueCollection ParseQueryString(Uri uri)
{
var result = new System.Collections.Specialized.NameValueCollection(4);
var query = uri.Query;
if (!String.IsNullOrEmpty(query))
{
var pairs = query.Substring(1).Split(\"&\".ToCharArray());
foreach (var pair in pairs)
{
var parts = pair.Split(\"=\".ToCharArray(), 2);
var name = System.Uri.UnescapeDataString(parts[0]);
var value = (parts.Length == 1) ? String.Empty : System.Uri.UnescapeDataString(parts[1]);
result.Add(name, value);
}
}
return result;
}
This is my code, I think it\'s very useful:
public String GetQueryString(string ItemToRemoveOrInsert = null, string InsertValue = null )
{
System.Collections.Specialized.NameValueCollection filtered = new System.Collections.Specialized.NameValueCollection(Request.QueryString);
if (ItemToRemoveOrInsert != null)
{
filtered.Remove(ItemToRemoveOrInsert);
if (!string.IsNullOrWhiteSpace(InsertValue))
{
filtered.Add(ItemToRemoveOrInsert, InsertValue);
}
}
string StrQr = string.Join(\"&\", filtered.AllKeys.Select(key => key + \"=\" + filtered[key]).ToArray());
if (!string.IsNullOrWhiteSpace(StrQr)){
StrQr=\"?\" + StrQr;
}
return StrQr;
}