ihorko ihorko - 4 years ago 234
ASP.NET (C#) Question

Can't decode cyrillic value from Request.QueryString

On my IIS7 I have ASP.NET WebForms site, and I use cyrillic values in the query string. I use

HttpUtility.UrlEncode
for params when do redirect, in the end I have url like:

http://mysite.com/Search.aspx?SearchText=текст


When I try to read param
SearchText
value (include
HttpUtility.Decode()
function) it returns me a wrong value of
ÑекÑÑ
, but should return
текст


It works on localhost on ASP.NET developer server, but doesn't on IIS7 (include local IIS7)
In my web.config I set up line

<globalization requestEncoding="utf-8" responseEncoding="utf-8" />


but it still doesn't work.

Appreciate any help,

Thanks a lot!

Answer Source

If you use the redirect function, yes inside it there is this call

url = UrlEncodeRedirect(url);

thats break the Cyrilic, Greece characters and probably others. If I remember well, (I say remember because this issue is from my experience some months ago) the break to the characters is after the ? symbol. In any case I have the same issue.

Possible solutions:

  • Make your custom redirect, maybe not so good as the original, but you can by pass this issue.
  • Find some alternative way to your redirect logic.
  • Make your custom text encode that use only valid url characters that are not change by the redirect, and then decodes them again back. The minous on that is that will be like hidden text and not visible readable search word.

This is the very basic of the redirect.

public static void RedirectSimple(string url, bool endResponse)
{
    HttpResponse MyResponse = HttpContext.Current.Response;

    MyResponse.Clear();
    MyResponse.TrySkipIisCustomErrors = true;
    MyResponse.StatusCode = 302;
    MyResponse.Status = "302 Temporarily Moved";
    MyResponse.RedirectLocation = url;

    MyResponse.Write("<html><head><title>Object moved</title></head><body>\r\n");
    MyResponse.Write("<h2>Object moved to <a href=\"" + url + "\">here</a>.</h2>\r\n");
    MyResponse.Write("</body></html>\r\n");

    if (endResponse){
        MyResponse.End();
    }
}

You can make it a function and try it to see if works correctly.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download