Question

I have a page that supports multiple languages (the user can change the language at any time).
I update the thread's culture based on the user selection in the Initialize Culture method.

I also update the response object based on the code page for the selected country and set its charset to the WebName of the encoding used (if i don't update the response object based on the code page, then the data bound to controls gets garbled). the resource files are also localized based on the code page.

After doing the above changes, the page is getting displayed correctly in the browser.

However, when the user enters data in one of the Asian languages, the text gets all messed up. The drop down list actually throws the invalid callback or postback argument exception.

On checking the request.contentEncoding i found that it was still UTF-8 (set in web.config).

If I change the request.ContentEncoding during BeginRequest event, the input comes in correctly. But I am not sure of the user selected language this early in the page cycle.

Is the above approach the correct way to go about it? How can i dynamically set the encoding for the incoming request?

Was it helpful?

Solution

The request is something a client have sent you. You cannot choose what he sends, it is your input. However, the client should probably be sending this input in the encoding defined on the page containing the form.

Do you set the charset correctly on the page? Are you sure you do not send e.g.

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

in the HTML header, while the data is in a different encoding?

Added after comments: If I understand it correctly, you are generating the output page in a different encoding than UTF-8 (are you sure you need to do that?). After which you have problems decoding the form data POSTed back. The simplest way would be to ensure the data gets posted back in UTF-8. For that, HTML defines the accept-charset attribute on form. However, the attribute is not supported in IE. For IE, there is a hack: you could try adding a fake <input> containing any “strange” Unicode character to force IE post as UTF-8.

Which is not very nice, a simpler solution would be simply to use UTF-8 always – do you really need to use different encodings for your output page?

Or, if you do need to output data in another encoding, your solution of setting Request.ContentEncoding is AFAIAA the only way you can use. To determine the encoding used, I would recommend adding a hidden field which is POSTed back (in theory, you could use HTTP headers, but I don’t think browsers send them with the POSTed data), to use something like this:

    public void Application_BeginRequest(object sender, EventArgs args)
    {
        var inputEncoding = Request.Form["hiddenEncoding"];
        if (!String.IsNullOrEmpty(inputEncoding)) Request.ContentEncoding = Encoding.GetEncoding(inputEncoding);
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top