Greetings!
I have a web service (ASMX) and in it, a web method that does some work and throws an exception if the input wasn't valid.
[ScriptMethod]
[WebMethod]
public string MyWebMethod(string input)
{
string l_returnVal;
if (!ValidInput(input))
{
string l_errMsg = System.Web.HttpUtility.HtmlEncode(GetErrorMessage());
throw new Exception(l_errMsg);
}
// some work gets done...
return System.Web.HttpUtility.HtmlEncode(l_returnVal);
}
Back in the client-side JavaScript on the Web page, on the error callback function, I display my error:
function GetInputErrorCallback(error)
{
$get('input_error_msg_div').innerHTML = error.get_message();
}
This works great and when my Web method returns (a string), it always looks perfect. However, if one of my error messages from a my thrown exception contains a special character, it's displayed incorrectly in the browser. For example, if the error message were to contain the following:
That input isn’t valid! (that's an ASCII #146 in there)
It displays this:
That input isn’t valid!
Or:
Do you like Hüsker Dü? (ASCII # 252)
Becomes:
Do you like Hüsker Dü?
The content of the error messages comes from XML files with UTF-8 encoding:
<?xml version="1.0" encoding="UTF-8"?>
<ErrorMessages>
<Message id="invalid_input">Your input isn’t valid!</Message>
.
.
.
</ErrorMessages>
And as far as page encoding is concerned, in my Web.config, I have:
<globalization enableClientBasedCulture="true" fileEncoding="utf-8" />
I also have an HTTP Module to set L10n parameters:
Thread.CurrentThread.CurrentUICulture = m_selectedCulture;
Encoding l_Enc = Encoding.GetEncoding(m_selectedCulture.TextInfo.ANSICodePage);
HttpContext.Current.Response.ContentEncoding = l_Enc;
HttpContext.Current.Request.ContentEncoding = l_Enc;
I've tried disabling this HTTP Module but the result is the same.
The values returned by the web service (in the l_errMsg variable) look fine in the VS debugger. It's just once the client script has a hold of, it displays incorrectly. I've used Firebug to look at the response and special characters are mangled in there, too. So I find it pretty strange that strings returned by my web method look fine, even if there's special characters in them. Yet when I throw an exception from the web method, special characters in its message are incorrect. How can I fix this? Thanks in advance.