I need to build a URL with a query string in C#. What is the best approach to doing this? Right now, I’m using something like,
string url = String.Format('foo.aspx?{0}={1}&{2}={3}', 'a', 123, 'b', 456);
Is there a better, preferred approach?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I think that is a good method if it’s always know what parameters you have, if this is unknown at the time you could always keep a List<KeyValuePair<string,string>> with the key being the name and value being the value, then build the query string using a foreach loop like
note: the code is not tested and has not been compiled, so it may not work exactly as is.