HTML encoding in the .NET class library

I have a utility class for sending emails. Letters are created as html.

I am having problems encoding special characters that do not display correctly.

However, I cannot decide how to use HtmlEncode , since I do not have the current HttpContext . Is there a .Net class that will translate special characters into html codes without running it in Asp.Net ?

I hope this makes sense, I am a little rustling about how it all works. Thanks!

+6
c # html-encode
source share
5 answers

You can import the System.Web namespace and use the HttpUtility class. Altohugh is a web build, I don’t think it needs to work under a web environment for it to work.

+10
source share

I believe that System.Net.WebUtility.HtmlEncode will provide this functionality. MSDN Link for WebUtility.HTMLEncode

+20
source share

You can use the HttpUtility class from System.Web.

+2
source share

You should really study the AntiXSS WPL library.

  • You do not need System.Web!

  • It uses the principle of including a white list (in the encoding System.Web uses a black listing). Only those characters that are known to be safe are available, and all others will be correctly encoded

+2
source share
0
source share

All Articles