fearofawhackplanet
fearofawhackplanet

Reputation: 53378

Html encoding in .NET class library

I have a utility class for sending emails. The emails are generated as html.

I'm having problems with encoding special characters which aren't getting displayed correctly.

However, I can't work out how to use HtmlEncode as I don't have a current HttpContext. Is there a .Net class which will translate special characters to html codes without running under Asp.Net?

I hope that makes sense, I'm a bit flaky about how all this stuff works. Thanks!

Upvotes: 7

Views: 10278

Answers (5)

Adam Spicer
Adam Spicer

Reputation: 2721

You should really look into using the AntiXSS library of the WPL.

  1. You don't need System.Web!

  2. It uses a white-list inclusion principle (System.Web encoding uses black-listing). Only characters that are known to be safe will be trusted and all others will be properly encoded

Upvotes: 2

JohnIdol
JohnIdol

Reputation: 50067

How about HttpUtility.HtmlEncode?

Upvotes: 0

Klaus Byskov Pedersen
Klaus Byskov Pedersen

Reputation: 120917

You can use the HttpUtility class from System.Web.

Upvotes: 2

You can import the System.Web namespace and use the HttpUtility class. Altohugh it's a Web assembly I don't think it's neccesary to be running under a Web environment for it to work.

Upvotes: 11

Dean Taylor
Dean Taylor

Reputation: 41981

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

Upvotes: 21

Related Questions