将JS'escape'd字符串转换回C#中的正确格式

时间:2011-01-07 16:19:34

标签: c# javascript json character-encoding asmx

好的我正在尝试将HTML标记发送回服务器(不要问)。我已经设法使用jQuery.ajax。但为了使它到达WebMethod,我必须使用JS的escape函数,因此<DIV>之类的内容最终看起来像%3CDIV%20。公平够了。

我敢肯定必须有一个内置的C#库来为我解决这些十六进制字符引用,当然?有人能告诉我它是什么吗?

我真的不想为此包含一些JSON库。

2 个答案:

答案 0 :(得分:1)

您的意思是 URL编码字符串

String DecodedString = Server.UrlDecode(EncodedString);

http://msdn.microsoft.com/en-us/library/6196h3wt.aspx

答案 1 :(得分:1)

对于HTML内容:

System.Web.HttpUtility.HtmlEncode(string);
System.Web.HttpUtility.HtmlDecode(string);

对于网址:

System.Web.HttpUtility.UrlEncode(string);
System.Web.HttpUtility.UrlDecode(string);