Get individual query parameters from Uri

Ghostrider picture Ghostrider · May 21, 2010 · Viewed 122k times · Source

I have a uri string like: http://example.com/file?a=1&b=2&c=string%20param

Is there an existing function that would convert query parameter string into a dictionary same way as ASP.NET Context.Request does it.

I'm writing a console app and not a web-service so there is no Context.Request to parse the URL for me.

I know that it's pretty easy to crack the query string myself but I'd rather use a FCL function is if exists.

Answer

Timothy Shields picture Timothy Shields · May 7, 2013

Use this:

string uri = ...;
string queryString = new System.Uri(uri).Query;
var queryDictionary = System.Web.HttpUtility.ParseQueryString(queryString);

This code by Tejs isn't the 'proper' way to get the query string from the URI:

string.Join(string.Empty, uri.Split('?').Skip(1));