decodeURIComponent() equivalent in C#

The Javascript decodeURIComponent()  function decodes a string that was URI encoded with the encodeURIComponent() Javascript function. If you wish to encode a string in C# that can be decoded with decodeURIComponent you have to use the EscapeDataString function:

System.Uri.EscapeDataString("This is my text");

The escaping an unescaping of strings can for example be used to add HTML strings to a Javascript array from C#. Adding HTML strings to an array using the ClientScript.RegisterArrayDeclaration() function will produce an error when the page is executed. But if you encode/decode the strings, the function will succeed:

protected void Page_Load(object sender, EventArgs e)
{
  StringBuilder sb = new StringBuilder();
  sb.AppendFormat("\"{0}\"", Uri.EscapeDataString("
<h1>This is my first heading</h1>
"));
  sb.AppendFormat("\"{0}\"", Uri.EscapeDataString("
<h2>This is my second heading</h2>
"));
  sb.AppendFormat("\"{0}\"", Uri.EscapeDataString("
<h3>This is my third heading</h3>
"));
  Page.ClientScript.RegisterArrayDeclaration("myArray", sb.ToString());
}

This function appends 3 HTML strings to an array and adds the array to the page. The strings are now accessible from Javascript. This pseudo function will take an ID of a DIV tag and the index of the array and output the text from the above created array in the DIV tag:

<script language="javascript" type="text/javascript">
  function ShowDescription(divID, index)
  {
    var divTag = document.getElementById(divID);
    divTag.innerHTML = decodeURIComponent( myArray[index] );
  }
</script>
About these ads

About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in General .NET and tagged , , . Bookmark the permalink.

6 Responses to decodeURIComponent() equivalent in C#

  1. alan says:

    UR the worlds smartest man :-)

  2. Andrew says:

    Finally an answer that was correct, many thanks

  3. Pingback: Javascript string encoding in C# « Brian Pedersen’s Sitecore and .NET Blog

  4. I tried it on “http%3A%2F%2Ftest.org%2Fapp%3Fpar%3Dval%26par2%3Dval2%23asd”
    Didn’t work for me. The example here does not prove the point. The input string is not necessarily an “encode(d)URIComponent”
    I just used HttpUtility.UrlDecode which worked for my purpose. I am using .NET4

  5. Btw, after reading this more carefully, my previous post assumed that the title is correct (equivalent of decodeURIComponent). But the demonstration is actually, at least supposedly, an equivalent of encodeURIComponent.
    But if you look at the following:

    https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeURIComponent

    http://msdn.microsoft.com/en-us/library/system.uri.escapedatastring.aspx

    They really are not equivalent in implementation and purpose.

  6. karthik says:

    HI,
    How to implement decode function in my wcf services. From mobile app they encoding the string and call my wcf method and we need to decode that string using c# can you please guide me to finish this up.

    Mobile app code to encode the string using javascript
    decodeURIComponent( any );

    wcf sample method:

    Public string methodname(string encodingstring)
    {
    //here i need to decode the string
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s