简体   繁体   中英

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!

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

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.

您可以使用System.Web中的HttpUtility类。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM