Skip to content Skip to sidebar Skip to footer

ASP.NET - What Characters Does Server.HtmlEncode Encode Into Named Character Entities

What characters does Server.HtmlEncode encode into named character entities? So far I have only found < > & and " surely there must be more than this

Solution 1:

This is the code of HtmlEncode, so here you can see how they done it.

public static unsafe void HtmlEncode(string value, TextWriter output)
{
    if (value != null)
    {
        if (output == null)
        {
            throw new ArgumentNullException("output");
        }
        int num = IndexOfHtmlEncodingChars(value, 0);
        if (num == -1)
        {
            output.Write(value);
        }
        else
        {
            int num2 = value.Length - num;
            fixed (char* str = ((char*) value))
            {
                char* chPtr = str;
                char* chPtr2 = chPtr;
                while (num-- > 0)
                {
                    chPtr2++;
                    output.Write(chPtr2[0]);
                }
                while (num2-- > 0)
                {
                    chPtr2++;
                    char ch = chPtr2[0];
                    if (ch <= '>')
                    {
                        switch (ch)
                        {
                            case '&':
                            {
                                output.Write("&amp;");
                                continue;
                            }
                            case '\'':
                            {
                                output.Write("&#39;");
                                continue;
                            }
                            case '"':
                            {
                                output.Write("&quot;");
                                continue;
                            }
                            case '<':
                            {
                                output.Write("&lt;");
                                continue;
                            }
                            case '>':
                            {
                                output.Write("&gt;");
                                continue;
                            }
                        }
                        output.Write(ch);
                        continue;
                    }
                    if ((ch >= '\x00a0') && (ch < 'Ā'))
                    {
                        output.Write("&#");
                        output.Write(ch.ToString(NumberFormatInfo.InvariantInfo));
                        output.Write(';');
                    }
                    else
                    {
                        output.Write(ch);
                    }
                }
            }
        }
    }
}

Solution 2:

.NET 4 and 4.5 encode single quotes also, which doesn't appear to be in the answer


Post a Comment for "ASP.NET - What Characters Does Server.HtmlEncode Encode Into Named Character Entities"