179

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?

svick
247k54 gold badges407 silver badges536 bronze badges
asked Jul 17, 2009 at 17:04
1
  • Related post - HtmlEncode in C# Commented Sep 6, 2021 at 14:40

7 Answers 7

213

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);
Doctor Jones
21.8k13 gold badges81 silver badges103 bronze badges
answered Jul 17, 2009 at 17:05
Sign up to request clarification or add additional context in comments.

3 Comments

You need to create an instance of the Server Utility class which is designed to support a current in progress Request and emulate features the old ASP Server object. HttpUtility is a lighter weight set of Static methods.
Make sure that your framework type does not specify "Client Profile". Changes this to the full framework and you'll have the system.web assembly available
System.Web.HttpUtility was not available in my project (.NET Framework 4.7.1). System.Net.WebUtility.HtmlEncode(string) was available and worked fine.
41

If you are using C#3 a good tip is to create an extension method to make this even simpler. Just create a static method (preferably in a static class) like so:

public static class Extensions
{
 public static string HtmlEncode(this string s)
 {
 return HttpUtility.HtmlEncode(s);
 }
}

You can then do neat stuff like this:

string encoded = "<div>I need encoding</div>".HtmlEncode();
answered Jul 17, 2009 at 20:03

Comments

29

Try this

System.Net.WebUtility.HtmlDecode(string);
System.Net.WebUtility.HtmlEncode(string);
answered Apr 3, 2012 at 14:19

1 Comment

This is much better because I don't have to add reference to System.Web in my WPF project.
7

Add a reference to System.Web.dll and then you can use the System.Web.HtmlUtility class

answered Jul 17, 2009 at 17:08

Comments

3

Just reference the System.Web assembly and then call: HttpServerUtility.HtmlEncode

http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.htmlencode.aspx

answered Jul 17, 2009 at 17:07

Comments

3

In case you're using SharePoint 2010, using the following line of code will avoid having to reference the whole System.Web library:

Microsoft.SharePoint.Utilities.SPHttpUtility.HtmlEncode(stringToEncode);
answered Apr 3, 2011 at 23:38

Comments

2

In case you are working with silverlight, use this:

System.Windows.Browser.HttpUtility.HtmlEncode(...);
answered Dec 11, 2010 at 2:41

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.