4

I am filling a textbox with values but for some reason "ÅÄÖ" letters converts to strange characters when I use QueryString. it happens only when I use QueryStringso the problem is not in my web config.

This is how it looks like:

this.txtNewName.Text = Page.Request.QueryString["RenameSite"];

aspx:

<asp:TextBox runat="server" CssClass="rename-web-textarea" ID="txtNewName" Visible="true" MaxLength="45" />

Value should be "ärta bårta örta"

but result is: ärta bårta örta

Is there any other ways of solving this? or any way to fix it so it works with "ÅÄÖ" ?

Note! This happens only with IE8 and IE10, it works with Chrome!

The request url is this:

?RenameWebTitle=%C3%A4rta%20b%C3%A5rta%20%C3%B6rta&RenameWebUrl=http://test/site/test&IsDlg=1"

I Captured the request by the IE dev tool and Here is some detailed info:

Accept-Language: sv-SE,sv;q=0.8,en-US;q=0.5,en;q=0.3
Response headers: Content-Type: text/html; charset=utf-8

Anyone have any idea how I can solve this? Its much appreciated!

Obsivus
  • 8,231
  • 13
  • 52
  • 97
  • Strange characters like what? Can you give an example? – Tobberoth Dec 11 '13 at 08:14
  • What it the exact query-string here? Are these values %-encoded? – Marc Gravell Dec 11 '13 at 08:18
  • What encoding does the browser claim to use when making that request? What encoding is the page containing the link in? You can see this by right-clicking the page and selecting the Encoding submenu. – CodeCaster Dec 11 '13 at 08:26
  • if the problem only happens with IE8/IE10, then it doesn't seem like a server-side issue; you should ideally be looking at the exact http, in particular: what is the query-string in the request? what is the encoding in the request? what is the encoding in the response? what is the body of the response? – Marc Gravell Dec 11 '13 at 08:28
  • @MarcGravell I updated with the request url – Obsivus Dec 11 '13 at 08:32
  • Definitely seems to be an IE issue, the urlencode is correct. Maybe somethings up with windows language settings? – Tobberoth Dec 11 '13 at 08:38
  • Please add all the request headers. It seems like the server is Url decoding using the wrong charset. With `var decoded = HttpUtility.UrlDecode("%C3%A4rta", Encoding.UTF8);` the result is `ärta`, while `var decoded = HttpUtility.UrlDecode("%C3%A4rta", Encoding.GetEncoding(1252));` give`s `ärta`. – user1429080 Dec 11 '13 at 11:09
  • @user1429080 could you make an example – Obsivus Dec 11 '13 at 11:44
  • @Obsivus I'm not able to reporoduce exactly what you describe, but if you have a `` element in your `web.config`, please append it to the question. Also add the `Request` headers (as opposed to the `Response` headers). Use for instance `Fiddler` to catch them... – user1429080 Dec 11 '13 at 13:21

1 Answers1

0

You need to encode your querystring parameters before sending them

string queryString = "RenameSite=" + encodeURIComponent("ÅÄÖ");
string url = "yoururl.com?" + queryString;
Tim Davis
  • 524
  • 6
  • 17
  • Please refer to the RFC which mentions that you _need_ to. – CodeCaster Dec 11 '13 at 08:22
  • The `Request.QueryString` is read-only, ASP.NET handles that automatically when working with forms. – Tobberoth Dec 11 '13 at 08:22
  • 1
    @Tobberoth the interesting question, perhaps, is what is the exact url that was *requested*, and was the incoming query-string well-formed; I think Tim is working on the principle that it probably isn't – Marc Gravell Dec 11 '13 at 08:23
  • @CodeCaster HTTP headers are US-ASCII (RFC 2616, if you really need it), so the characters ÅÄÖ don't exist. *Something* will need to encode them, and if not the OP, then who? –  Dec 11 '13 at 08:30
  • @hvd I know what happens at the request level, but that is not what this question or Tims answer is about. Tim shows a piece of code that encodes a part of a query string, most probably before printing it to HTML, which (with the proper encoding) is not necessary. The browser will percent-encode the URL in the request when you click it. – CodeCaster Dec 11 '13 at 08:35
  • http://en.wikipedia.org/wiki/Query_string#URL_encoding.. You cannot rely on the browser to encode your querystring as the request may not come from a browser – Tim Davis Dec 11 '13 at 08:36
  • @Tim [http://www.josscrowcroft.com/2011/code/utf-8-multibyte-characters-in-url-parameters-✓/](http://www.josscrowcroft.com/2011/code/utf-8-multibyte-characters-in-url-parameters-✓/), [Unicode characters in URLs](http://stackoverflow.com/questions/2742852/unicode-characters-in-urls). Anyway, can you explain what you think your code does and when it should be used? – CodeCaster Dec 11 '13 at 08:38
  • @CodeCaster before the OP modified his answer, my code was meant to show how to encode non ascii characters for use in a URL. Now that he has added that he indeed encoded it first, and his charset is UTF-8 this may be a bug in IE.. Try using HttpContext.Current.Request.RawUrl – Tim Davis Dec 11 '13 at 08:48
  • @CodeCaster and that link you refer to has a couple of quotes "I have no idea if this would work cross-browser (I’m on Chrome) or cross-platform" and more interestingly, "Might be a disaster, in the wild. But heck – try it out and see what happens" – Tim Davis Dec 11 '13 at 08:50