C# · Method · ASP.net
HtmlEncode before writing text that came from a user
C# code to HTML-encode text before writing it to a web page, so user-typed content cannot break the page or inject scripts.
How to use HtmlEncode before writing text that came from a user:
Paste the code into the class or page, then call it from the method that needs the result.Encode at the point you write HTML. Encoding twice shows & to the user. Text stored in Class Act needs this when placed inside a div.// using System.Web;
string safe = HttpUtility.HtmlEncode(text);