Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET WebService Returns Gibberish Characters When Throwing Exceptions
    text
    copied!<p>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.</p> <pre><code>[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); } </code></pre> <p>Back in the client-side JavaScript on the Web page, on the error callback function, I display my error:</p> <pre><code>function GetInputErrorCallback(error) { $get('input_error_msg_div').innerHTML = error.get_message(); } </code></pre> <p>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:</p> <p><strong>That input isn’t valid!</strong> (that's an ASCII #146 in there)</p> <p>It displays this:</p> <p><strong>That input isn’t valid!</strong></p> <p>Or:</p> <p><strong>Do you like Hüsker Dü?</strong> (ASCII # 252)</p> <p>Becomes:</p> <p><strong>Do you like Hüsker Dü?</strong></p> <p>The content of the error messages comes from XML files with UTF-8 encoding:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;ErrorMessages&gt; &lt;Message id="invalid_input"&gt;Your input isn’t valid!&lt;/Message&gt; . . . &lt;/ErrorMessages&gt; </code></pre> <p>And as far as page encoding is concerned, in my Web.config, I have:</p> <pre><code>&lt;globalization enableClientBasedCulture="true" fileEncoding="utf-8" /&gt; </code></pre> <p>I also have an HTTP Module to set L10n parameters:</p> <pre><code>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; </code></pre> <p>I've tried disabling this HTTP Module but the result is the same.</p> <p>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?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload