thegunner
thegunner

Reputation: 7163

Get parameter from url string

If I have a URL but as a string e.g. www.example.com?q=1234&h=4567 how can I pick out e.g. "q"

I'm picking the url up from a database so I can't use request.querystring("q")

Upvotes: 1

Views: 71

Answers (2)

jbl
jbl

Reputation: 15413

I would try:

HttpUtility.ParseQueryString(new Uri("http://www.example.com?q=1234&h=4567").Query).Get("q")

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460258

You can use HttpUtility.ParseQueryString:

string url = new Uri("http://www.example.com?q=1234&h=4567").Query;
System.Collections.Specialized.NameValueCollection nvc = System.Web.HttpUtility.ParseQueryString(url);
foreach (string key in nvc.AllKeys)
{
     // ...
}

(note that i've added the "http" to the url, otherwise you could not create an Uri)

Upvotes: 3

Related Questions