Escape/ Unescape With UrlEncode/UrlDecode In ASP.NET/ jQuery

This blog tells how to send an HTML string to a controller action using Escape and use that string in a Controller action using UrlEncode.

Recently, I faced an issue while sending an HTML string as a parameter to the Controller action. What I wanted was to have two textareas and a button on my View. And then, get the values from textareas and send it to the Controller.
 
Then, I was going to save these values to a file at the specific location (like JS, HTML, CSS, Txt), and if the file was already there at the location, then read that file and show data in the respective textarea.
 
What problem did I face?
 
When I tried to send a string which had HTML tags like '<br>, <b>xxxxxxx</b>', I used an AJAX call to send the data from View to Controller as below.
  1. function SaveDataToFile() {  
  2.     var JsCode = $('#textarea1').val();  
  3.     var JsCode2 = $('#textarea2').val();  
  4.     $.ajax({  
  5.         type: "POST",  
  6.         url: "@Url.Action("  
  7.         xxxAction ", "  
  8.         xxController ")",  
  9.         datatype: "JSON",  
  10.         data: {  
  11.             JsCode: JsCode,  
  12.             FileName: xxx,  
  13.             JsCode2: JsCode2,  
  14.             yyy: yyy  
  15.         },  
  16.         beforeSend: function() {  
  17.             alert("Before send to controller");  
  18.         },  
  19.         success: function() {  
  20.             alert("Success");  
  21.             setTimeout(function() {  
  22.                 location.reload();  
  23.             }, 4000);  
  24.         },  
  25.     });  
  26. }  
The above function is a JS function and it will work absolutely fine if JsCode/ JsCode2 variables have plane text
(i.e., no HTML tags in it).
 
If in those variables you have HTML tags ('<br>, <b>xxxxx</b>, <h1>xxxx</h1>'), then it will not give you any error in console window but it goes in the success of that AJAX method and shows alert as "Success".
This happens because while sending a parameter to the controller (in my above case, there are 4 parameters), the AJAX call will convert it to the JSON format because in the variable (JsCode/JsCode2), it will have HTML code it will not be able to convert to JSON format.
 
The value in Textarea as,
 
 
 
After calling JS function  SaveDataToFile 
in JSCode, the variable value will come as follows.
 
 
To avoid this problem there is a Jquery in build function available that we can use to convert string to serializable string. These functions can be used as:
  1. JsCode = escape(JsCode);  
  2. JsCode2 = escape(JsCode2);   
The above line of code will convert the value from textarea as,
 
 
 
You need to use this before calling Action from the controller.
  1. function SaveDataToFile() {  
  2.     var JsCode = $('#textarea1').val();  
  3.     var JsCode2 = $('#textarea2').val();  
  4.     JsCode = escape(JsCode);  
  5.     JsCode2 = escape(JsCode2);  
  6.     $.ajax({  
  7.         type: "POST",  
  8.         url: "@Url.Action("  
  9.         xxxAction ", "  
  10.         xxController ")",  
  11.         datatype: "JSON",  
  12.         data: {  
  13.             JsCode: JsCode,  
  14.             FileName: xxx,  
  15.             JsCode2: JsCode2,  
  16.             yyy: yyy  
  17.         },  
  18.         beforeSend: function() {  
  19.             alert("Before send to controller");  
  20.         },  
  21.         success: function() {  
  22.             alert("Success");  
  23.             setTimeout(function() {  
  24.                 location.reload();  
  25.             }, 4000);  
  26.         },  
  27.     });  
  28. }  
The above value of variable JSCode will easily convert to JSON format and send to Controller action. Call will go to the controller action and there you need to again decode the above as an encoded string to Normal; you can use C# function like this:
  1. JsCode = HttpUtility.UrlDecode(JsCode);  
  2. JsCode2 = HttpUtility.UrlDecode(JsCode2); 
This UrlDecode will convert an encoded string to the normal string as,

 
Now, this is in Controller, then you can use it as you want.
 
In my case I'm using this to create JS file Dynamically,
 
__HAPPY_CODING__