cjs*_*rp1 10 asp.net encoding internet-explorer utf-8 query-string
在尝试显示URL查询字符串中提供的特殊字符(德语重音字符)时,我在Internet Explorer中发现了一个奇怪的问题,特别是IE9.这在Firefox和Chrome中正常运行.
例如,我正在使用的URL看起来像这样:
http://mysite.com/TestPage.aspx?Title=Hochauflösendes®
Run Code Online (Sandbox Code Playgroud)
我也尝试过URL的URL编码版本:
http://mysite.com/TestPage.aspx?Title=Hochaufl%C3%B6sendes%C2%AE
Run Code Online (Sandbox Code Playgroud)
在任何一种情况下,当我尝试在我的页面上显示"标题"查询字符串值时Request.QueryString["Title"],IE不会正确显示字符:
Hochaufl?sendes?
Run Code Online (Sandbox Code Playgroud)
如果我直接在页面上对文本进行硬编码,它将在所有浏览器上正确显示.只有当它从问题发生的查询字符串中拉出时才会出现.
该页面保存为UTF-8编码,我meta在页面中有必要的标签:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
Run Code Online (Sandbox Code Playgroud)
我也通过Fiddler查看了页面的标题和内容,所有编码都是正确的.
什么可能导致IE不能正确显示特殊字符?
正如阿里斯托斯所建议的,使用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);
}
Run Code Online (Sandbox Code Playgroud)
使用此方法检索值经测试在 IE8 和 IE9 中有效。该错误在 IE10 中已修复。
| 归档时间: |
|
| 查看次数: |
11455 次 |
| 最近记录: |