50

Are the any functions in C# that handle escape/unescape like JavaScript?

I have a JSON string like this:

{"Feeds":[{"Url":"www.test.com","FeedType":"Twitter"},{"Url":"www.test2.com","FeedType":"Youtube"}]}

Which looks like this after escape()

%7B%22Feeds%22%3A%5B%7B%22Url%22%3A%22www.test.com%22%2C%22FeedType%22%3A%22Twitter%22%7D%2C%7B%22Url%22%3A%22www.test2.com%22%2C%22FeedType%22%3A%22Youtube%22%7D%5D%7D

In my C# code I would like to unescape this string so that it looks exactly the same as it did before the escape()

Is this possible?

7 Answers 7

74

HttpUtility.UrlDecode should do the trick.

Sign up to request clarification or add additional context in comments.

8 Comments

@Timwi - You are correct. I mis-read. Updated. Just out of curiosity, why did you roll your own code (now deleted) to do the same thing instead of re-using what .NET provides?
Thx, I tried HttpUtility.HtmlDecode but it did not work, but when I see your answer I see that UrlDecode does something that HtmlDecode did not do.
This doesn't handle the '+' char. (Try using UrlDecode on "abc+def" it seems to be replacing the + with a space.) Temporarily I am using Microsoft.JScript.GlobalObject.unencode() But I hate bringing in a dependency just for that...
"+" is a space character in a URL. It's up to the encoder to escape that as well. In JavaScript you want to use encodeURIComponent instead of escape, which also covers the + character.
This is not decoding swedish characters properly- å gets escaped to %E5 and decoded to: �
|
22

escape() is equivalent to

HttpUtility.UrlDecode(str, System.Text.Encoding.Default);

By default the UrlDecode uses UTF8 while escape() don't.

5 Comments

UTF8 is backward compatible with ASCII so, unless I'm mis-understanding something, you shouldn't have to pass in the encoding.
This is wrong... escape() is actually equivalent to HttpUtility.UrlDecode(str, Encoding.Default), which means two things: ① it depends on your system locale settings; and ② it is obsolete 1990s 8-bit technology and incompatible with UTF-8. (The second paragraph is correct though.)
@Justin Niessner: As you said, it is backward compatible. Not equivalent. And I want to give emphasis on backward, don't even think it's compatible the other way.
HttpUtility.UrlDecode(msg, System.Text.Encoding.Default); Works for me actually
for languages with special chars this did the trick, thank you sir
11

This is the best way I found to work with these:

Encode in C#:

System.Uri.EscapeDataString("<string>");

Decode in JavaScript:

decodeURI("<string>");

Encode in JavaScript:

encodeURI("<string>");

Decode in C#:

System.Uri.UnescapeDataString("<string>");

Update 27-Jan-2016: Just found what seems do be a more compatible way to do it, which also encodes the URI protocol (http://) using javascript:

Encode in JavaScript:

encodeURIComponent("<string>");

Decode in JavaScript:

decodeURIComponent("<string>");

Comments

5

Aw man, why do we over-think stuff so much sometimes. When an API function is being silly, send a karma cuss at the library developer, then work-around it...

HttpUtility.UrlEncode(editext, System.Text.Encoding.Default).Replace("+","%20");

Comments

2
    internal static string UnJavascriptEscape(string s)
    {
        // undo the effects of JavaScript's escape function
        return HttpUtility.UrlDecode(s.Replace("+", "%2b"), Encoding.Default);
    }

Comments

1

To unescape without having to reference System.Web in order to use HttpUtility, try this:

Str = Str.Replace("+", " ");
Str = Regex.Replace(Str, "%([A-Fa-f\\d]{2})", a => "" + Convert.ToChar(Convert.ToInt32(a.Groups[1].Value, 16)));

Also, when I tried HttpUtility.UrlDecode, it didn't work for special characters áéíóúñ.

Comments

0

I spent 8 hours trying to get

HttpUtility.UrlDecode 

to work, and gave up and used

HttpUtility.HtmlDecode

which worked instantly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.