【发布时间】:2012-06-14 06:34:00
【问题描述】:
Server.HtmlEncode 将哪些字符编码为命名字符实体?
目前我只找到< > & and ",肯定不止这些吧?
【问题讨论】:
标签: asp.net html character-encoding webforms w3c
Server.HtmlEncode 将哪些字符编码为命名字符实体?
目前我只找到< > & and ",肯定不止这些吧?
【问题讨论】:
标签: asp.net html character-encoding webforms w3c
这是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("&");
continue;
}
case '\'':
{
output.Write("'");
continue;
}
case '"':
{
output.Write(""");
continue;
}
case '<':
{
output.Write("<");
continue;
}
case '>':
{
output.Write(">");
continue;
}
}
output.Write(ch);
continue;
}
if ((ch >= '\x00a0') && (ch < 'Ā'))
{
output.Write("&#");
output.Write(ch.ToString(NumberFormatInfo.InvariantInfo));
output.Write(';');
}
else
{
output.Write(ch);
}
}
}
}
}
}
【讨论】:
.NET 4 和 4.5 也编码单引号,这似乎不在答案中
【讨论】:
case '\'':