Why can't I use HttpServerUtility.HtmlEncode inside a class?

markzzz picture markzzz · Apr 21, 2012 · Viewed 16.6k times · Source

I am trying to use the following code:

string myString = HttpServerUtility.HtmlEncode("my link & details");

I am getting the following error:

An object reference is required for the nonstatic field, method, or property.

Why can't I use HttpServerUtility.HtmlEncode inside a class?

Answer

jorgebg picture jorgebg · Apr 21, 2012

You can use HttpUtility instead, which has a static method that does not depend on HttpContext.

string myString = HttpUtility.HtmlEncode("my link & details");

More info on HttpUtility.HtmlEncode method on the MSDN.