UrlEncode without HttpContext

by Damiaan Peeters 25. August 2009 12:19

Yesterday I had to use the UrlEncode function.  UrlEncoding is used to converts all illegal characters in a URL to valid characters.  For example the space will be converted to %20.

Fortunately the .Net Framework supplies a UrlEncode method on its Server Class.  The server class resides in the HttpContext.  The problem is that you need a HttpContext Instance to call this class.

Using Reflector, I found out that the UrlEncode method on the Server Property uses the HttpUtility class.


So, instead of using this: HttpContext.Current.Server.UrlEncode(text)
you can also use: HttpUtility.UrlEncode(text)

The only thing to use this is to include the System.Web assembly in your project.


Certified Umbraco Master, Part of Umbraco Certified partner comm-it, .Net and Azure developer, seo lover. Magician in my spare time.

Month List