Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

A common task when calling web resources from a code is building a query string to including all the necessary parameters. While by all means no rocket science, there are some nifty details you need to take care of like, appending an & if not the first parameter, encoding the parameters etc.

The code to do it is very simple, but a bit tedious:

StringBuilder SB = new StringBuilder();
if (NeedsToAddParameter A) 
{ 
  SB.Append("A="); SB.Append(HttpUtility.UrlEncode("TheValueOfA")); 
}

if (NeedsToAddParameter B) 
{
  if (SB.Length>0) SB.Append("&"); 
  SB.Append("B="); SB.Append(HttpUtility.UrlEncode("TheValueOfB")); }
}

This is such a common task one would expect a utility class to exist that makes it more elegant and readable. Scanning MSDN, I failed to find one—which brings me to the following question:

What is the most elegant clean way you know of doing the above?

share|improve this question
17  
It's a bit sad that even at the current point in time, there seems to be no straightforward way to deal with querystrings. And by straightforward, I mean an OOB, non-internal, standards-compliant framework class. Or maybe I'm missing out on something? – Grimace of Despair Dec 1 '13 at 21:27
3  
You're not missing anything. Querystring building is a major gap in the framework that I've tried to fill with Flurl. – Todd Menier May 7 '14 at 14:37
    

31 Answers 31

EDIT - as pointed out in the comments, this is not the way to go.

There is such a class - the URI Class. "Provides an object representation of a uniform resource identifier (URI) and easy access to the parts of the URI." (Microsoft docs).

The following example creates an instance of the Uri class and uses it to create a WebRequest instance.

C# example

Uri siteUri = new Uri("http://www.contoso.com/");

WebRequest wr = WebRequest.Create(siteUri);

Check it out, there are lots of methods on this class.

share|improve this answer
    
-1: He asked about the query string. – John Saunders May 6 '09 at 11:30
    
the Uri class has no methods to manage the query string, other then getting it and setting it (and I'm not sure about the latter) – Guss May 6 '09 at 11:32
    
The Uri class is good once you have a URI built including the query. Uri is immutable so you can't add to it once it's created. There is the UriBuilder class, but IIRC it doesn't have a method for query string; it's still left to the programmer to create it. The Uri class is good once you have it constructed for things like proper escaping. – ageektrapped May 6 '09 at 11:32
    
My appologies - I stand corrected. – JonnyBoats May 6 '09 at 11:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.