【问题标题】:ASP.NET - What Characters does Server.HtmlEncode Encode into Named Character EntitiesASP.NET - Server.HtmlEncode 将哪些字符编码为命名字符实体
【发布时间】:2012-06-14 06:34:00
【问题描述】:

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

目前我只找到< > & and ",肯定不止这些吧?

【问题讨论】:

    标签: asp.net html character-encoding webforms w3c


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      .NET 4 和 4.5 也编码单引号,这似乎不在答案中

      【讨论】:

      • 答案是单引号:case '\'':
      • 我的立场是正确的!我可以辩解说六年前我更年轻更笨,但这在技术上只是真实的:)
      猜你喜欢
      • 1970-01-01
      • 2011-09-10
      • 2011-07-27
      • 1970-01-01
      • 2012-02-23
      • 2012-04-02
      • 1970-01-01
      • 2016-07-05
      • 2017-09-22
      相关资源
      最近更新 更多