About Me · Send mail to the author(s) E-mail · Twitter

At GROSSWEBER we practice what we preach. We offer trainings for modern software technologies like Behavior Driven Development, Clean Code and Git. Our staff is fluent in a variety of languages, including English.

Feed Icon


Open Source Projects


Blogs of friends

Now playing [?]

Error retrieving information from external service.


Building UTF-8 Compatible QueryStrings (Not Only) for ASP.NET

Posted in ASP.NET | Browser at Friday, 22 September 2006 20:53 W. Europe Daylight Time

Whenever you append a query string to an URL, it's a best practice to URL-encode the query string value. On the server side encoding can be done by three methods of the System.Web.HttpUtility class, depending on the desired output.

Here's an example passing "Encoding Test: abcäöüß" to the methods:

Method Output
HttpUtility.UrlEncode Encoding+Test%3a+abc%c3%a4%c3%b6%c3%bc%c3%9f
HttpUtility.UrlEncodeUnicode Encoding+Test%3a+abc%u00e4%u00f6%u00fc%u00df
HttpUtility.UrlPathEncode Encoding%20Test:%20abc%c3%a4%c3%b6%c3%bc%c3%9f

The single method HttpUtility.UrlDecode() decodes all encoded query strings.

On the client side there are JavaScript functions to encode and decode query strings, namely escape() and unescape(). However, these functions have problems with the correct encoding of UTF-8 strings.

Today I had the same problem described in the article above while debugging search in dasBlog, which uses UTF-8 as the default request and response encoding. You can enable UTF-8 for any ASP.NET web site by including the following element in your web.config.

<globalization requestEncoding="utf-8" responseEncoding="utf-8" />

All search queries containing umlauts were retrieved without umlauts on the server side, so a query for König would result in Knig. The solution was to leverage the newer encoding functions encodeURIComponent() and decodeURIComponent() respectively which handle Unicode.

If you're interested in encoding in general I recommend Scott Hanselman's excellent podcast on globalization and internationalization. It gives a good overview without diving too deep into the details.

By the way, I recommend having a look at, I really like their fast Ajax-enabled search engine for references of C, C++, CSS, HTML, HTML DOM, Java, JavaScript, MySQL, Perl, PHP and Ruby.

Now playing: New Order - Get ready - Vicious streak

Saturday, 23 September 2006 00:44:40 (W. Europe Daylight Time, UTC+02:00)
So does HttpUtility.UrlEncode use UTF-8 if you have that config setting while HttpUtility.UrlEncodeUnicode use UTF-16?
Saturday, 23 September 2006 11:12:07 (W. Europe Daylight Time, UTC+02:00)
It appears to be the case. Using an overload of UrlEncode you can also specify the Encoding to use. A good overview of UTF-8 advantages and disadvantages compared to other encodings can be found here.
Thursday, 07 February 2008 20:45:29 (W. Europe Standard Time, UTC+01:00)
I'm having some problems in decoding urls on IIS6.
When I click on URL UTF-8 encoded, querystring replaces any non english chars with question marks. For example, if you open you can see a link in the middle of the page containing an UTF-8 char properly encoded . If you click on it, results page shows incorrect querystring "404;" (see upper left corner)
In fact, the unicode char has been replaced with a question mark. Why?
As you can see I use a 404 custom handler to manage all tags and all pages involved use UTF-8

Thank you
Saturday, 15 November 2008 09:08:55 (W. Europe Standard Time, UTC+01:00)
Nice Solution
thank you Amigo!
Monday, 15 November 2010 12:04:25 (W. Europe Standard Time, UTC+01:00)
I was having problems with chars like ã because i was using encodeURIComponent(escape(myvalue)). On the server side i'm using Microsoft.JScript.GlobalObject.decodeURI(). Works great, your article made the "click" to solve my problem. Thanks.
All comments require the approval of the site owner before being displayed.
(will show your gravatar icon)
[Captcha]Enter the code shown (prevents robots):

Live Comment Preview