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

1

2 Answers 2

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)

Sign up to request clarification or add additional context in comments.

Comments

1

I would try:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.