开发者

Make serverside strings safe for use in clientside scripts

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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜