我正在尝试使用RegisterStartUpScript.NET 3.5中的MS ScriptManager 的方法将SQL Server异常的输出传递给客户端.这适用于某些错误,但当异常包含单引号时,警报失败.
我不想只是逃避单引号.是否有一个标准函数我可以调用以逃避任何特殊字符在JavaScript中使用?
string scriptstring = "alert('" + ex.Message + "');";
ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", scriptstring , true);
Run Code Online (Sandbox Code Playgroud)
编辑:
谢谢@tpeczek,代码几乎为我工作:)但稍微修改(单引号的转义)它是一种享受.
我在这里包括了我的修改版本......
public class JSEncode
{
/// <summary>
/// Encodes a string to be represented as a string literal. The format
/// is essentially a JSON string.
///
/// The string returned includes outer quotes
/// Example Output: "Hello \"Rick\"!\r\nRock on"
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static string EncodeJsString(string s)
{ …Run Code Online (Sandbox Code Playgroud)