asp.net - Internet Explorer 9 中 URL 查询字符串值的 UTF-8 字符编码问题

标签 asp.net internet-explorer encoding utf-8 query-string

当尝试显示 URL 查询字符串中提供的特殊字符(德语重音字符)时,我在 Internet Explorer(特别是 IE9)中发现了一个奇怪的问题。 这在 Firefox 和 Chrome 中按预期工作。

例如,我正在使用的 URL 如下所示:

http://mysite.com/TestPage.aspx?Title=Hochauflösendes®

我还尝试了 URL 的 URL 编码版本:

http://mysite.com/TestPage.aspx?Title=Hochaufl%C3%B6sendes%C2%AE

无论哪种情况,当我尝试使用 Request.QueryString["Title"] 在页面上显示“标题”查询字符串值时,IE 都无法正确显示字符:

Hochaufl�sendes�

如果我直接在页面上对文本进行硬编码,它就会在所有浏览器上正确显示。只有当它从出现问题的查询字符串中提取时才会出现这种情况。

页面保存为 UTF-8 编码,并且我的页面中有必要的 meta 标记:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>

我还通过 Fiddler 查看了页面的标题和内容,所有编码都是正确的。

什么可能导致 IE 无法正确显示特殊字符?

最佳答案

按照 Aristos 的建议,使用 HttpContext.Current.Request.RawUrl 适合我的情况。

要从 RawUrl 检索实际的查询字符串值,可以使用如下简单方法:

private string GetQueryStringValueFromRawUrl(string queryStringKey)
{
    var currentUri = new Uri(HttpContext.Current.Request.Url.Scheme + "://" + 
        HttpContext.Current.Request.Url.Authority + 
        HttpContext.Current.Request.RawUrl);
    var queryStringCollection = HttpUtility.ParseQueryString((currentUri).Query);
    return queryStringCollection.Get(queryStringKey);
}

使用此方法检索值经过测试,可以在 IE8 和 IE9 中正常工作。该错误已在 IE10 中修复。

关于asp.net - Internet Explorer 9 中 URL 查询字符串值的 UTF-8 字符编码问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14636073/

相关文章:

c# - 应用程序 [Win32Exception (0x80004005) : The system cannot find the file specified] with model first EF

javascript - IE 中的事件未定义在 Firefox 中有效

c# - 如何在 C# 中对字符串进行 URL 编码

c# - 将凭据传递给 Sql Report Server 2008

asp.net - 断点不会被击中

asp.net - IIS 7 如何将客户端证书映射到数据库中的用户帐户?

java - 从 String 中删除不适合 UTF-8 编码的字符

internet-explorer - 如何让 PIE(IE 的 CSS3)工作?

jquery ui datepicker Internet Explorer CSS问题

java - 无法通过 chrome/firefox 获取请求参数值。与双重转义有关?