Problems with UTF-8 character encoding from URL qu

2019-04-21 15:16发布

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®

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

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

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.

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"/>

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

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

1条回答
男人必须洒脱
2楼-- · 2019-04-21 15:53

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

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

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

查看更多
登录 后发表回答