What is the correct encoding for querystrings? What is the correct encoding for querystrings? asp.net asp.net

What is the correct encoding for querystrings?


It looks like you're applying UrlEncode over the entire URL - this isn't correct, paths and query strings are encoded differently as you've seen. What is doing the encoding of the URI, WebRequest?

You could manually build the various parts using a UriBuilder, or manually encode using UrlPathEncode for the path and UrlEncode for the query string names and values.

Edit:

If the problem lies in the path, rather than the query string you could try turning on IRI support, via web.config

<configuration>  <uri>      <iriParsing enabled="true" />  </uri></configuration>

That should then leave international characters alone in the path.


I ended up changing my remote webservice to expect the querystring to be UTF-8 encoded. It solves my immediate problem, the webservice can not be correctly called by both PHP and the .NET framework.

However, the behavior is now strange in browsers. Copy pasting an url like "http://mysite.dk/tv%C3%A6rs?test=%C3%A6" into the browser and then pressing return works, it even corrects the encoded characters and displays the location as "http://mysite.dk/tværs?test=æ". If then reload the page (F5) it still works. But if I click on the location bar and press return again, the querystring will become encoded with latin-1 and fail.

For anyone interested here is an old Firefox bugreport about the problem: https://bugzilla.mozilla.org/show_bug.cgi?id=284474 (thanks to @dtb)

So, it seems there is no good solution.

Thanks to everyone who helped though!