1

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")

Cœur
  • 37,241
  • 25
  • 195
  • 267
thegunner
  • 6,883
  • 30
  • 94
  • 143
  • possible duplicate of [C# ASP.NET QueryString parser](http://stackoverflow.com/questions/574868/c-sharp-asp-net-querystring-parser) – Oded Dec 12 '12 at 13:10

2 Answers2

3

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)

Cœur
  • 37,241
  • 25
  • 195
  • 267
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
1

I would try:

HttpUtility.ParseQueryString(new Uri("http://www.example.com?q=1234&h=4567").Query).Get("q")
Cœur
  • 37,241
  • 25
  • 195
  • 267
jbl
  • 15,179
  • 3
  • 34
  • 101