如何转义在C#中使用的html文本?我想做的是
sample="<span>blah<span>"并拥有
<span>blah<span>显示为纯文本,而不是仅包含html :(.使用C#而非ASP
发布于 2009-06-17 05:36:16
using System.Web;
var encoded = HttpUtility.HtmlEncode(unencoded);发布于 2009-12-20 05:59:05
此外,如果您不想使用System.Web程序集,也可以使用以下代码:
var encoded = System.Security.SecurityElement.Escape(unencoded)根据this article,System.Security.SecurityElement.Escape()和System.Web.HttpUtility.HtmlEncode()之间的区别在于前者还对撇号(')字符进行编码。
发布于 2013-09-16 16:22:17
如果您使用的是.NET 4或更高版本,并且不想引用System.Web,则可以使用System中的WebUtility.HtmlEncode
var encoded = WebUtility.HtmlEncode(unencoded);这与HttpUtility.HtmlEncode具有相同的效果,应该优先于System.Security.SecurityElement.Escape。
https://stackoverflow.com/questions/1005264
复制相似问题