Javascript’s escape() in C# – a C# equivalent to the escape()-function

csharp_vs_javascriptThe .NET framework provides a large number of ways to encode HTML code and URLs. There exist, for example, Uri.EscapeDataString(), Uri.EscapeUriString(), System.Web.HttpUtility.UrlEncode(), System.Web.HttpUtility.UrlPathEncode(), System.Web.HttpUtility.HtmlEncode() or System.Web.HttpUtility.HtmlAttributeEncode().

However, if you are looking for equivalent functionality to Javascript’s escape(), so you will be disappointed by all of these aforementioned functions. None of these inherently with the .NET framework delivered functions is equivalent to the Javascript escape() function.  For better understanding, I made the following example:

The test text is in german, because the ‘ä’ is a nice character to show the problems. Translated to english it means: “Raffi’s annoying C# testcode”.

// Original source text
// Raffi's ärgerlicher C# Teststring/Testcode.

// Javascript original (produced by escape())
// Raffi%27s%20%E4rgerlicher%20C%23%20Teststring/Testcode.

// Uri.EscapeDataString():
// Raffi's%20%C3%A4rgerlicher%20C%23%20Teststring%2FTestcode.

// Uri.EscapeUriString():
// Raffi's%20%C3%A4rgerlicher%20C#%20Teststring/Testcode.

// System.Web.HttpUtility.UrlEncode():
// Raffi%27s+%c3%a4rgerlicher+C%23+Teststring%2fTestcode.

// System.Web.HttpUtility.UrlPathEncode():
// Raffi's%20%c3%a4rgerlicher%20C#%20Teststring/Testcode.

// System.Web.HttpUtility.HtmlEncode():
// Raffi's ärgerlicher C# Teststring/Testcode.

// System.Web.HttpUtility.HtmlAttributeEncode():
// Raffi's ärgerlicher C# Teststring/Testcode.

But I would not blog about it if I did not have a solution up my’s sleeve for you. And this is not all that complicated.

Step 1:

Add a “Microsoft.JScript” reference to your project .

microsoft jscript verweis hinzufügen

Step 2:

Now you can call the Microsoft.JScript.GlobalObject.escape() function in your code. This one converts the given string to exactly as it is done by the normal Javascript escape() function also. (This is because by this method you use to access the Microsoft’s JScript engine.) By using this function the above shown example works perfectly.

// Original source text
// Raffi's ärgerlicher C# Teststring/Testcode.

// Javascript original (produced by escape())
// Raffi%27s%20%E4rgerlicher%20C%23%20Teststring/Testcode.

// Microsoft.JScript.GlobalObject.escape():
// Raffi%27s%20%E4rgerlicher%20C%23%20Teststring/Testcode.

I hope I could help one or the other of you. You might know another or perhaps better way in C# to get an equivalent to Javascipt’s  escape() function? Then write me a comment!

1 Comments

  1. colinsays:

    Note: escape() javascript function is deprecated now. Using encodeURI and encodeURIComponent.
    So if you wanted a url of “http://website.com/my fun site.aspx?comment=Raffi’s ärgerlicher C# Teststring/Testcode”

    You’d go:
    var url = encodeURI(“http://website.com/raffi’s fun page.aspx?comment=”) + encodeURIComponent(“Raffi’s ärgerlicher C# Teststring/Testcode”)
    which would give you:
    http://website.com/raffi's%20fun%20page.aspx?comment=Raffi's%20%C3%A4rgerlicher%20C%23%20Teststring%2FTestcode

    From .net, i’m not sure how you’d do it best… maybe this isn’t where you wanted the blog post to go, though…

Leave a comment

Please be polite. We appreciate that. Your email address will not be published and required fields are marked