Contact

admin

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

Tags

Open Source Projects

Archives

Blogs of friends

Now playing [?]

Error retrieving information from external service.
Audioscrobbler/Last.fm

ClustrMap

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

Posted in ASP.NET | Browser at Friday, September 22, 2006 8:53 PM 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 QuickRef.org, 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, September 23, 2006 12:44:40 AM (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, September 23, 2006 11:12:07 AM (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, February 07, 2008 8:45:29 PM (W. Europe Standard Time, UTC+01:00)
Hello,
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 http://www.qmpeople.com/ you can see a link in the middle of the page http://www.qmpeople.com/tag/%D1%84 containing an UTF-8 char properly encoded . If you click on it, results page shows incorrect querystring "404;http://www.qmpeople.com:80/tag/?" (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, November 15, 2008 9:08:55 AM (W. Europe Standard Time, UTC+01:00)
Nice Solution
thank you Amigo!
Monday, November 15, 2010 12:04:25 PM (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