views:

159

answers:

3
+6  A: 

You need to encode your values by calling Server.UrlEncode before putting them into the URL.

If your values are in an array, you'll need to use a loop. If you want precise instructions, you'll need to show us your current code.

SLaks
+1 although where I work it's common practise to use Server.UrlEncode instead.
Ed Woodcock
If you're writing a client app, you might not have System.Web referenced.
SLaks
+6  A: 

The ampersand (&) is a special character in a url. So the above URL is being interprested as www.google.com with three arguments: 2 Departments and a Finance.

Try using:

System.Web.HttpUtility.UrlEncode(string url) 

to encode the URL.

Jeff Hornby
Hi Jeff Could you tell me how to use it with NameValueCollection..as the main problem is I am using that to get the Name and Value from the query string....
TSSS22
You need to use UrlEncode when you're creating the querystring. You can encode each piece of the URL separately before concatenating them together: string URL = "www.google.com?" + HttpUtility.UrlEncode("Department=Education
Jeff Hornby
+2  A: 

The values in the query string should be url encoded. Now there is no way to determine the difference between the & characters that separate the value pairs and the & character inside one of the values. Also, eventhough most browsers will accept spaces in an URL, that is invalid according to the standard.

In the value Education & Finance you have to encode the spaces as + or %20, and the & character as %26 so that it becomes Education%20%26%20Finance.

So the correct URL should be:

www.google.com?Department=Education%20%26%20Finance&Department=Health
Guffa