Does C# Have an Equivalent to JavaScript's Encodeuricomponent()

Does C# have an equivalent to JavaScript's encodeURIComponent()?

Uri.EscapeDataString or HttpUtility.UrlEncode is the correct way to escape a string meant to be part of a URL.

Take for example the string "Stack Overflow":

  • HttpUtility.UrlEncode("Stack Overflow") --> "Stack+Overflow"

  • Uri.EscapeUriString("Stack Overflow") --> "Stack%20Overflow"

  • Uri.EscapeDataString("Stack + Overflow") --> Also encodes "+" to "%2b" ---->Stack%20%2B%20%20Overflow

Only the last is correct when used as an actual part of the URL (as opposed to the value of one of the query string parameters)

Equivalent to Javascript's encodeURI?

Try

Uri.EscapeUriString("http://www.mysite.com/my cool page")

Is there any equivalent to JavaScript's decodeURIComponent() in C#?

You can try in this way at server side:

string encodedValue="abc%2B123";
string decodedvalue= Uri.UnescapeDataString(encodedValue);
Console.WriteLine(decodedvalue);
-- or
string encodedValue1=HttpUtility.HtmlEncode("abc+123");
string decodedValue1 = HttpUtility.HtmlDecode(encodedValue1);
Console.WriteLine(decodedValue1);

Thanks

What is the JavaScript equivalent of C# Server.URLEncode?

encodeURI()

http://xkr.us/articles/javascript/encode-compare/#ref-js-msdn

Cope with differences, using c# and javascript methods while encoding URI parts

Percent-encoding is defined in RFC3986. It defines that a space character is encoded as %20. However, query strings are most often in application/x-www-form-urlencoded format, which also allows encoding spaces as + instead of %20. If the decoder expects the query string in this format, then an encoder can choose whichever it prefers.

You can use the Uri.EscapeDataString Method if you want spaces always to be encoded as %20:

var result = Uri.EscapeDataString("a q");
// result == "a%20q"

On upper-case vs lower-case, RFC3986 says:

The uppercase hexadecimal digits 'A' through 'F' are equivalent to
the lowercase digits 'a' through 'f', respectively. If two URIs
differ only in the case of hexadecimal digits used in percent-encoded
octets, they are equivalent.

For consistency, URI producers and
normalizers should use uppercase hexadecimal digits for all
percent-encodings.

var result = Uri.EscapeDataString("a+q");
// result == "a%2Bq"

What is the JavaScript equivalent of C#'s HttpServerUtility.UrlTokenDecode?

I finally managed to convert the Objective-c version of URLTokenDecode by Jeffrey Thomas to JavaScript and it worked.

Here is the function:

function URLTokenDecode(token) {

if (token.length == 0) return null;

// The last character in the token is the number of padding characters.
var numberOfPaddingCharacters = token.slice(-1);

// The Base64 string is the token without the last character.
token = token.slice(0, -1);

// '-'s are '+'s and '_'s are '/'s.
token = token.replace(/-/g, '+');
token = token.replace(/_/g, '/');

// Pad the Base64 string out with '='s
for (var i = 0; i < numberOfPaddingCharacters; i++)
token += "=";

return token;
}

Here is the $filter if you are using AngularJS:

app.filter('URLTokenDecode', function () {
return function (token) {

if (token.length == 0) return null;

// The last character in the token is the number of padding characters.
var numberOfPaddingCharacters = token.slice(-1);

// The Base64 string is the token without the last character.
token = token.slice(0, -1);

// '-'s are '+'s and '_'s are '/'s.
token = token.replace(/-/g, '+');
token = token.replace(/_/g, '/');

// Pad the Base64 string out with '='s
for (var i = 0; i < numberOfPaddingCharacters; i++)
token += "=";

return token;
}

});

UrlEncode - Javascript vs. C#

System.Web.HttpUtility.UrlEncode will use UTF8 (i think..) as its default encoder, you can change this by specifying one..

System.Web.HttpUtility.UrlEncode("éléphant", Encoding.Default); // %e9l%e9phant

Though it may be preferable to specify an actual codepage or what not, instead of relying on the OS default.



Related Topics



Leave a reply



Submit