///// <summary> ///// Produce a string in double quotes with backslash sequences in all the right places. ///// ///// 常用的用法:String.Format("{0}.setValue({1});", ClientJavascriptID, JsHelper.Enquote(Text)) ///// 大部分情况下,可以使用 GetJsString 函数代替此函数 ///// 此函数返回的是双引号括起来的字符串,用来作为JSON属性比较合适,一般用在OnAjaxPreRender ///// 但是作为HTML属性时,由于HTML属性本身就是双引号括起来的,就容易引起冲突 ///// ///// </summary> ///// <param name="s">A String</param> ///// <returns>A String correctly formatted for insertion in a JSON message.</returns> /// <summary> /// 返回的是双引号括起来的字符串,用来作为JSON属性比较合适 /// </summary> /// <param name="s">源字符串</param> /// <returns>双引号括起来的字符串</returns> public static string Enquote(string s) { string jsonString = new JValue(s).ToString(Formatting.None); // The browser HTML parser will see the </script> within the string and it will interpret it as the end of the script element. // http://www.xiaoxiaozi.com/2010/02/24/1708/ // http://stackoverflow.com/questions/1659749/script-tag-in-javascript-string jsonString = jsonString.Replace("</script>", @"<\/script>"); return jsonString; /* if (s == null || s.Length == 0) { return "\"\""; } char c; int i; int len = s.Length; StringBuilder sb = new StringBuilder(len + 4); string t; sb.Append('"'); for (i = 0; i < len; i += 1) { c = s[i]; if ((c == '\\') || (c == '"') || (c == '>')) { sb.Append('\\'); sb.Append(c); } else if (c == '\b') sb.Append("\\b"); else if (c == '\t') sb.Append("\\t"); else if (c == '\n') sb.Append("\\n"); else if (c == '\f') sb.Append("\\f"); else if (c == '\r') sb.Append("\\r"); else { if (c < ' ') { //t = "000" + Integer.toHexString(c); string tmp = new string(c, 1); t = "000" + int.Parse(tmp, System.Globalization.NumberStyles.HexNumber); sb.Append("\\u" + t.Substring(t.Length - 4)); } else { sb.Append(c); } } } sb.Append('"'); return sb.ToString(); * */ }
private static void EncryptJsonValue(JsonPasswordCrypto cryptoHandler, JValue valueToEncrypt) { var value = valueToEncrypt.Value<string>(); var encryptedValue = cryptoHandler.Encrypt(value); valueToEncrypt.Replace(JObject.FromObject(encryptedValue)); }