ASP.NET - Server.HtmlEncode 将哪些字符编码为命名字符实体

标签 asp.net html character-encoding webforms w3c

Server.HtmlEncode 将哪些字符编码为命名字符实体?

到目前为止我只找到了< > &和"肯定不止这些?

最佳答案

这是HtmlEncode的代码,所以在这里你可以看到他们是如何做到的。

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);
                    }
                }
            }
        }
    }
}

关于ASP.NET - Server.HtmlEncode 将哪些字符编码为命名字符实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10978673/

相关文章:

c# - 为什么 Image.FromFile 返回 System.Drawing.Bitmap?

c# - 尝试加载 Crystal Reports 运行时时出错

html - 背景属性在 CSS 中表现异常

javascript - 如何调整按钮的断点,使它们折叠成一列?

python - Python默认字符串编码

python - (Linux) iwatch 在文件名中使用特殊字符执行 python

c# - 如何在 Asp.Net 网站中包含另一个项目控制台应用程序 exe?

asp.net - 将 OpenIDConnect 集成到经典 ASP 应用程序中

javascript - 如何使用 jQuery 在 html 元素上设置 CSS 属性?

php - PHP x MYSQL 使用 UTF8_encode() 和 UTF8_decode() 时出现重音错误?