Search code examples
vb.nethtml-encode

HtmlEncode Isn't Encoding Properly


It doesn't appear that the strings are being encoded.

Issue:

Dim encodedStr As String = HttpUtility.HtmlEncode("<p>This is a test</p>")

The output produces:

<p>This is a test</p>

Expected result:

&lt;p&gt;This is a test&lt;/p&gt;

Solution

  • I'd double check to make sure whatever is displaying the value is indeed displaying it in text mode, and not HTML. Under the debugger, it works fine:

    HttpUtility.HtmlEncode("<p>This is a test</p>");
    "&lt;p&gt;This is a test&lt;/p&gt;"
    

    (Yes, this is C# but the .NET framework code is all shared and I was lazy)