.NET类库中的Html编码
我有一个用于发送电子邮件的实用程序类。电子邮件生成为 html。
我在编码无法正确显示的特殊字符时遇到问题。
但是,我无法弄清楚如何使用 HtmlEncode
,因为我没有当前的 HttpContext
。是否有一个 .Net
类可以将特殊字符转换为 html 代码,而无需在 Asp.Net
下运行?
我希望这是有道理的,我对所有这些东西的工作原理有点不稳定。谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
HttpUtility.HtmlEncode 怎么样?
How about HttpUtility.HtmlEncode?
我相信 System.Net.WebUtility.HtmlEncode 将提供此功能。 WebUtility.HTMLEncode 的 MSDN 参考
I believe
System.Net.WebUtility.HtmlEncode
will provide this functionality. MSDN reference for WebUtility.HTMLEncode您可以导入 System.Web 命名空间并使用 HttpUtility 类。尽管它是一个 Web 程序集,但我认为没有必要在 Web 环境下运行它才能工作。
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.
您可以使用
HttpUtility
来自 System.Web 的类。You can use the
HttpUtility
class from System.Web.您确实应该考虑使用 WPL 的
AntiXSS
库。您不需要System.Web!
它使用白名单包含原则(System.Web 编码使用黑名单)。只有已知安全的字符才会被信任,所有其他字符都会被正确编码
You should really look into using the
AntiXSS
library of the WPL.You don't need System.Web!
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