Does C # have an equivalent JavaScript encodeURIComponent ()?

In JavaScript:

encodeURIComponent("©√") == "%C2%A9%E2%88%9A" 

Is there an equivalent for C # applications? To escape HTML characters, I used:

 txtOut.Text = Regex.Replace(txtIn.Text, @"[\u0080-\uFFFF]", m => @"&#" + ((int)m.Value[0]).ToString() + ";"); 

But I'm not sure how to convert the match to the correct hex format that JS uses. For example, this code:

 txtOut.Text = Regex.Replace(txtIn.Text, @"[\u0080-\uFFFF]", m => @"%" + String.Format("{0:x}", ((int)m.Value[0]))); 

Returns " %a9%221a" for "©√" instead of "%C2%A9%E2%88%9A" . It seems like I need to split the string into bytes or something like that.

Edit: this is a Windows application, the only elements available on System.Web are: AspNetHostingPermission , AspNetHostingPermissionAttribute and AspNetHostingPermissionLevel .

+84
javascript c # windows encoding
Sep 17 '08 at 19:05
source share
7 answers

Uri.EscapeDataString or HttpUtility.UrlEncode is the right way to avoid the string intended for the part of the URL.

Take, for example, the line "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 <the last is correct if used as the actual part of the URL (as opposed to the value of one of the query string parameters)

+138
Dec 29 2018-10-12-29
source share

HttpUtility.HtmlEncode / Decoding
HttpUtility.UrlEncode / Decoding

You can add a reference to the System.Web assembly if it is not available in your project

+15
Sep 17 '08 at 19:06
source share

I tried to execute a full compatible javascript analog of encodeURIComponent for C #, and after my 4 hour experiments, I found this

C # CODE:

 string a = "!@#$%^&*()_+ some text here   "; a = System.Web.HttpUtility.UrlEncode(a); a = a.Replace("+", "%20"); 

result: !% 40% 23% 24% 25% 5e% 26 * () _% 2b% 20some% 20text% 20here% 20% d0% b0% d0% bb% d0% b8% 20% d0% bc% d0% b0% d0% bc% d0% b5% d0% b4% d0% be% d0% b2% 20% d0% b1% d0% b0% d0% BA% D1% 83

After decoding with Javascript decodeURLComponent ();

You will receive the following :! @ # $% ^ & * () _ + text here Ali Mamedov Baku

Thanks for your attention

+13
Feb 04 2018-11-21T00:
source share

Try Server.UrlEncode() or System.Web.HttpUtility.UrlEncode() for instances if you do not have access to the Server object. You can also use System.Uri.EscapeUriString() to not add a reference to the System.Web assembly.

+9
Sep 17 '08 at 19:07
source share

System.Uri.EscapeUriString () did not seem to do anything, but System.Uri.Escape Data strong> String () worked for me.

+8
Mar 06 '09 at 18:46
source share

You can use the Server object in the System.Web namespace

Server.UrlEncode, Server.UrlDecode, Server.HtmlEncode and Server.HtmlDecode.

Edit: The poster added that this is a Windows application, not a web application, as you might believe. The items listed above will be available from the HttpUtility class inside System.Web, which should be added as a reference to the project.

+4
Sep 17 '08 at 19:06
source share

For the Windows Store app, you will not have HttpUtility. Instead, you have:

For the URI before the '?' Character:

  • System. Uri.EscapeUriString ("example.com/Qaru ++?")
    • → "example.com/Stack%20Overflow ++?"

For the name or value of the URI request after '?':

  • System. Uri.EscapeDataString ("Stack ++")
    • → "Stack% 20Overflow% 2B% 2B"

For x-www-form-urlencoded, the name or value of the request in the contents of the POST:

  • System.Net. WebUtility.UrlEncode ("Stack ++")
    • → "Stack + Overflow% 2B% 2B"
+4
Jun 10 '13 at 8:51
source share