Internet Explorer 9中URL查询字符串值的UTF-8字符编码问题 [英] Problems with UTF-8 character encoding from URL query string value in Internet Explorer 9

查看:239
本文介绍了Internet Explorer 9中URL查询字符串值的UTF-8字符编码问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Internet Explorer中找到一个奇怪的问题,特别是IE9,当尝试显示URL查询字符串中提供的特殊字符(德文重音字符)时。

I'm finding an odd issue in Internet Explorer, specifically IE9, when trying to display special characters (German accented characters) provided within the URL query string. This is working as expected in Firefox and Chrome.

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

For example, the URL I'm working with looks something like this:

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

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

I've also tried the URL encoded version of the URL:

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

在这两种情况下,当我尝试使用 Request.QueryString [Title] 在我的页面上显示标题查询字符串值时,IE不正确显示字符:

In either case, when I'm trying to display that 'Title' query string value on my page using Request.QueryString["Title"], IE doesn't display the characters correctly:

Hochaufl�sendes�

如果我直接在页面上直接编码文本,它会在所有浏览器上正常显示。只有当它从查询字符串发生问题的地方。

If I hard-code the text directly on the page, it displays properly on all browsers. It's only when it's pulling from the query string where the issue occurs.

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

The page is saved as UTF-8 encoding, and I have the meta tag in my page as necessary:

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

我也看过页面的标题和内容Fiddler,所有的编码是正确的。

I've also looked at the page's header and content via Fiddler, and all the encoding is correct.

可能导致IE不能正确显示特殊字符?

What could be causing IE not to display the special characters properly?

推荐答案

根据Aristos的建议,使用 HttpContext.Current.Request.RawUrl 为我的情况工作。

As suggested by Aristos, using HttpContext.Current.Request.RawUrl worked for my situation.

要从RawUrl中检索实际的查询字符串值,可以使用这样一个简单的方法:

To retrieve the actual query string value from the RawUrl, a simple method like this can be used:

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

Re使用这种方法进行测试,在IE8和IE9中工作。 IE10中修复了错误。

Retrieving the value using this method was tested as working in IE8 and IE9. The bug is fixed in IE10.

这篇关于Internet Explorer 9中URL查询字符串值的UTF-8字符编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆