views:

35

answers:

2

I have created an ASP.NET class. In that class i would like to use the Server.UrlEncode.

Why intellisense is not helping me at all and instead of Server.UrlEncode it displays the HttpServerUtility?

I have already a reference to system.web

+1  A: 

Because the .Server property of the page is an instance of HttpServerUtility class.

http://msdn.microsoft.com/en-us/library/system.web.ui.page.server.aspx

To use UrlEncode method outside the page, use the HttpUtility class.

http://msdn.microsoft.com/en-us/library/1e55w41w.aspx

Strelok
Wow! HttpContext.Current.Server.UrlEncode(StringToEncode) Thank you!
Chocol8
That too. But HttpUtility will also let you use those functions completely out of HttpContext.
Strelok
+1  A: 

You can access that function through the HttpContext object. I guess your class is in a class library in which you should always check you have a context in case your code is called outside of a web context. Try this:

if (HttpContext.Current != null)
{
    string sEncondedBit = HttpContext.Current.Server.UrlEncode("text & more txt");
}
Naeem Sarfraz