C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Note: HTML uses some characters such as < and >. These characters are interpreted differently than others.
Here: In this example, the string literal value1 is assigned to an HTML string containing escaped HTML characters.
And: If you have unencoded user input, you must escape it before rendering it to another web page or HTTP response.
Warning: Make sure to test that these methods cause no "malicious encoding" warnings before deploying the application.
Page that uses HttpUtility methods: C#
using System;
using System.Diagnostics;
using System.Web;
using System.Web.UI;
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
//
// Shows how the HtmlDecode and HtmlEncode methods work.
//
string value1 = "<html>";
string value2 = HttpUtility.HtmlDecode(value1);
string value3 = HttpUtility.HtmlEncode(value2);
Debug.WriteLine(value1);
Debug.WriteLine(value2);
Debug.WriteLine(value3);
}
}
Debug output:
<html>
<html>
<html>
And: The HtmlEncode method translated the decoded string back to the original encoded string.
Also: The other ways to encode data in System.Web may require actual Server intrinsic objects in the ASP.NET pipeline.