I have class
items whose unique identifier is their string
field. I am set these elements in html using Razor(C#). I handle it for javascript. I need to convert strings from example
"Mr. Smith" to id
"{2342-2341-2324-...}" or at Base64
"92384098230...". And then use javascript to get the value from the encoded string. How to do it in C# and Javascript?
Asked
Active
Viewed 843 times
0

Nik Volk
- 47
- 6
-
1Does this answer your question? [How can you encode a string to Base64 in JavaScript?](https://stackoverflow.com/questions/246801/how-can-you-encode-a-string-to-base64-in-javascript) – Liftoff Sep 16 '21 at 03:53
-
No! I was trying to do this before I asked the question. In Razor: System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(; In Js: atob( – Nik Volk Sep 16 '21 at 04:02
1 Answers
1
You could do the following-
C# - Uri.EscapeDataString(String)
And to convert the above value in JavaScript-
js - unescape(string) or decodeURI(string)
If you would like to do the reverse you can use
C# - Uri.UnEscapeDataString(string) & js - escape(string) or encodeURI (string)

cursorrux
- 1,382
- 4
- 9
- 20

Joel Pusanur
- 34
- 2
-
Thank you, this is what need. But it doesn't work - when converting back, I get Ðвод%20инÑоÑмаÑии – Nik Volk Sep 16 '21 at 04:16
-
Can you share a bit more details? C# what's is the original text and escaped text and in js unescaped text? – Joel Pusanur Sep 16 '21 at 04:20
-
1I did it! The "unescape" method is deprecated, I replaced it with "decodeURI": C#:`Uri.EscapeDataString(item.Name)` => Javascript:`decodeURI(itemId)`. Its work! Thanks! – Nik Volk Sep 16 '21 at 04:24
-
Added decodeURI in the answer - however https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/unescape – Joel Pusanur Sep 16 '21 at 04:36
-
1