You can access this function through an HttpContext object. I think your class is in a class library, in which you should always check that you have a context if your code is called outside of the web context. Try the following:
if (HttpContext.Current != null)
{
string sEncondedBit = HttpContext.Current.Server.UrlEncode("text & more txt");
}
source
share