UrlEncode Overview

The URLEncode service is used to take a url string and replace special characters with a % sign followed by their hex code. For example an = sign is replace with %3d.  This is frequently used when encrypting query string parameters to pass in the url. The encrypted string contains special characters that will cause issues in the url, so we encode the string and replace those characters with their hex code.

URLEncode Use Examples

In this example, we are going to look at the Manage Payment Profiles WebPart. This WebPart displays an Iframe that contains payment methods and it uses URLEncode to remove special characters from an encrypted string of parameters being passed to the IFrame window.

The Payment Pad section has an onload process. In this process we are running an Encrypt service that is encrypting a set of parameters that will then be passed in the URL of the IFrame. After the Encrypt service, we are running the URLEncode service and this takes the encrypted string and replaces special characters that may cause issues in a url and replaces them with a % sign followed by the special characters hexcode. Finally, the SetControlProperty service is setting the HTML of the HtmlGenericControl field on the webpart canvas.

Below you can see how the UrlEncode service is replacing special characters with their hexcode. ( / is turned into %2f and + is turned into %2a).

Finally, the SetControlProperty sets the HTML of the HTMLGenericControl and creates an IFrame of EP_ProcessPayment.aspx and we pass the encrypted and encoded parameters to the IFrame as a querystring of EP_Params.