HtmlEncode from Class Library

Villager picture Villager · Jul 17, 2009 · Viewed 128.4k times · Source

I have a class library (in C#). I need to encode my data using the HtmlEncode method. This is easy to do from a web application. My question is, how do I use this method from a class library that is being called from a console application?

Answer

Russ Bradberry picture Russ Bradberry · Jul 17, 2009

Import System.Web Or call the System.Web.HttpUtility which contains it

You will need to add the reference to the DLL if it isn't there already

string TestString = "This is a <Test String>.";
string EncodedString = System.Web.HttpUtility.HtmlEncode(TestString);