gpt4 book ai didi

asp.net - HttpUtility.HtmlEncode 转义太多?

转载 作者:行者123 更新时间:2023-12-04 12:42:42 25 4
gpt4 key购买 nike

在我们的 MVC3 ASP.net 项目中,HttpUtility.HtmlEncode 方法似乎转义了太多字符。我们的网页作为 UTF-8 页面提供,但该方法仍然会转义诸如 ü 或日元字符 ¥ 之类的字符,即使 tese 字符是 UTF-8 set 的一部分.

因此,当我的 asp.net MVC View 包含以下代码时:

    @("<strong>ümlaut</strong>")

然后我希望编码器转义 html 标签,而不是 ümlaut
    &lt;strong&gt;ümlaut&lt;/strong&gt;

但相反,它给了我以下一段 HTML:
    &lt;strong&gt;&#252;mlaut&lt;/strong&gt;

为了完整起见,我还提到 web.config 中的 responseEncoding 被明确设置为 utf-8,所以我希望 HtmlEncode 方法尊重这个设置。
    <globalization requestEncoding="utf-8" responseEncoding="utf-8" />

最佳答案

是的,我的网页也面临同样的问题。
如果我们看到 htmlEncode 的代码,就有翻译这组字符的点。这是这种字符也翻译的代码。

if ((ch >= '\x00a0') && (ch < 'A'))
{
output.Write("&#");
output.Write(ch.ToString(NumberFormatInfo.InvariantInfo));
output.Write(';');
}
else
{
output.Write(ch);
}

这是 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)
{
output.Write(chPtr2[0]);
chPtr2++;
}
while (num2-- > 0)
{
char ch = chPtr2[0];
if (ch <= '>')
{
switch (ch)
{
case '&':
{
output.Write("&amp;");
chPtr2++;
continue;
}
case '\'':
{
output.Write("&#39;");
chPtr2++;
continue;
}
case '"':
{
output.Write("&quot;");
chPtr2++;
continue;
}
case '<':
{
output.Write("&lt;");
chPtr2++;
continue;
}
case '>':
{
output.Write("&gt;");
chPtr2++;
continue;
}
}
output.Write(ch);
chPtr2++;
continue;
}
// !here is the point!
if ((ch >= '\x00a0') && (ch < 'Ā'))
{
output.Write("&#");
output.Write(ch.ToString(NumberFormatInfo.InvariantInfo));
output.Write(';');
}
else
{
output.Write(ch);
}
chPtr2++;
}
}
}
}
}

a 可能的解决方案是制作自定义 HtmlEncode,或使用 MS 的 Anti-Cross Site 脚本。

http://msdn.microsoft.com/en-us/security/aa973814

关于asp.net - HttpUtility.HtmlEncode 转义太多?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9129141/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com