将JS的转义字符串转换为c#中的正确格式

时间:2022-10-22 11:38:38

Ok I am trying to send HTML markup back to the server (don't ask). I have managed to do it using jQuery.ajax. But in order for it to reach the WebMethod I have to use JS's escape function so things like <DIV> end up looking like %3CDIV%20. Fair Enough.

好的,我正在尝试将HTML标记发送回服务器(不要问)。我已经使用jQuery.ajax实现了这一点。但是为了让它到达WebMethod,我必须使用JS的escape函数,所以像

这样的东西看起来就像%3CDIV%20。很好。

I'm sure there must be a built in C# library that will resolve these hex character references for me, surely? Can someone tell me what it is?

我确信在c#库中一定有一个构建,可以解决这些十六进制字符的引用,对吗?谁能告诉我是什么吗?

I really don't want to include some JSON library just for this.

我真的不想仅仅为了这个而包含一些JSON库。

2 个解决方案

#1


1  

You mean URL Encoded string.

你的意思是URL编码的字符串。

String DecodedString = Server.UrlDecode(EncodedString);

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

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

#2


1  

For html content:

html内容:

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

For urls:

网址:

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

#1


1  

You mean URL Encoded string.

你的意思是URL编码的字符串。

String DecodedString = Server.UrlDecode(EncodedString);

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

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

#2


1  

For html content:

html内容:

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

For urls:

网址:

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