How to mark ampersand and apostrophe safe in AntiXssEncoder?

213 Views Asked by At

Same a this question, but my issue is different.

I have a string with and & and ' that displaying their reference codes on the webpage. I need to get it to show the original characters, not the codes.

AntiXssEncoder.HtmlEncode("Mike's & Sallies Shop", true)

Output:

Mike's & Sallies Shop
1

There are 1 best solutions below

1
MARS On

For others who might have this problem, I found Decoding the string to work. My new code is below:

StringWriter myWriter = new StringWriter();
HttpUtility.HtmlDecode(AntiXssEncoder.HtmlEncode("Mike's & Sallies Shop", true), myWriter);
<p>myWriter.toString()</p>

Output is:

Mike's & Sallies Shop

EDIT: and even easier solution:

System.Net.WebUtility.HtmlDecode(AntiXssEncoder.HtmlEncode("Mike's & Sallies Shop", true))