I need to show the server exception in the client browser, but it seems there are some illegal characters inside the exp.Message
which makes the client script to raise "unterminated string literal" error.
any idea to make strings such as exp.Message
safe for client scripts?
catch (Exception exp) {
string __script = string.Format(@"alert('{0}');", exp.Message);
ScriptManager.RegisterStartupScrip开发者_JAVA百科t(this, this.GetType(), "theError", __script, true);
}
after a lot of search, finally found a solution in rick strahl's blog: EncodeJsString
Try HttpUtility.HtmlEncode
using System.Web;
...
var encodedMessage = HttpUtility.HtmlEncode(exp.Message);
string __script = string.Format(@"alert('{0}');", encodedMessage);
精彩评论