Basically a dup of this question using php, but I need it for C#.
I need to be able to replace any &
that is not currently not any HTML entity开发者_如何学C (e.g. &
) before outputting to screen. I was thinking a regex, but I'm not sure if .Net has something built in that will do this.
You can use HttpUtility.HtmlEncode
.
Whithing the context of a page or UserControl, you can use Server.HtmlEncode
.
Better AntiXss.HtmlEncode
, prevents XSS.
You could always HTML Decode the string (which would turn any HTML symbols into their display equivalents), replace any &'s, and then Encode the string again (which turns the symbols back into what they were originally). You might need to watch for side effects though.
精彩评论