Server Utility Functions for Non-Web Apps

This is just a quick post. Normally, when one needs to use such functions as HtmlEncode, HtmlDecode, UrlEncode, UrlDecode and related functions, one would normally go to the Server class in your current HttpContext.

However, in the case that you're running in a console application for any particular reason, you don't have an HttpContext.Current, and you would then throw a "Object reference not set to an instance of an object" (aka Null Reference Exception).

The correct way to do this is to use the System.Web.HttpUtility class. This utility is the one used internally by the Server object.

Recent comments