Wednesday, March 28, 2012

What is the .Net equivalent of jscript escape/unescape?

Is there a set of methods in the .Net framework that do the same thing as th
e
jscript functions escape and unescape?
I get very close with HttpUtility.UrlEncodeUnicode, but it puts "+" for
spaces instead of "%20". Uri.EscapeString seems to create the correct escap
e
string, but there is no unescape. The function is not public anyway, so I
probably shouldn't use it.
I need a way to escape text on the client, and then unescape it on the
server, and then escape it on the server, and unescape it out on the client.
How can I do this? Thanks.Hello,
Because UrlEncode gets very close, I wrote my own methods that just wraps
that and adds some extra functionality (like replacing the apostrophe ' to
avoid cross-site-scripting warnings).
I provide sample code and charts of what UrlEncode escapes at this blog entr
y:
http://timstall.dotnetdevelopersjou...ead/1092094.htm
For example:
public static string UrlFullEncode(string strUrl)
{
if (strUrl == null)
return "";
strUrl = System.Web.HttpUtility.UrlEncode(strUrl);
return strUrl.Replace("'",_strApostropheEncoding);
}
private const string _strApostropheEncoding = "%27";
public static string UrlFullDecode(string strUrl)
{
if (strUrl == null)
return "";
strUrl = strUrl.Replace(_strApostropheEncoding,"'");
return System.Web.HttpUtility.UrlDecode(strUrl);
}
"Harry Keck" wrote:

> Is there a set of methods in the .Net framework that do the same thing as
the
> jscript functions escape and unescape?
> I get very close with HttpUtility.UrlEncodeUnicode, but it puts "+" for
> spaces instead of "%20". Uri.EscapeString seems to create the correct esc
ape
> string, but there is no unescape. The function is not public anyway, so I
> probably shouldn't use it.
> I need a way to escape text on the client, and then unescape it on the
> server, and then escape it on the server, and unescape it out on the clien
t.
> How can I do this? Thanks.

0 comments:

Post a Comment