How to encode URLs containing Unicode? I would like to pass it to a command line utility and I need to encode it first.
Example: http://zh.wikipedia.org/wiki/白雜訊
becomes http://zh.wikipedia.org/wiki/%E7%99%BD%E9%9B%9C%E8%A8%8A.
-
It seems Stackoverflow text editor encoded Unicode url. I would like to do the same in c#. Click on the link to get actual Unicode url.Tomas– Tomas2011年09月07日 13:33:41 +00:00Commented Sep 7, 2011 at 13:33
-
2Stack Overflow didn’t do this – your browser did! It displays the URL as Unicode but when you copy it, the copied text contains the URL-encoded string.Konrad Rudolph– Konrad Rudolph2011年09月07日 13:36:01 +00:00Commented Sep 7, 2011 at 13:36
-
@KonradRudolph My browser, however, did not. I see it as what I presume to be chinese characters. :)The Dag– The Dag2011年11月14日 17:10:49 +00:00Commented Nov 14, 2011 at 17:10
-
@TheDag That’s a misconception: the browser may still display the URL as Unicode, but internally it’s URL-encoded. To check this, try copying the Unicode URL from the address bar and pasting it into a text field (but not the address bar).Konrad Rudolph– Konrad Rudolph2011年11月14日 17:55:34 +00:00Commented Nov 14, 2011 at 17:55
4 Answers 4
You can use the HttpUtility.UrlPathEncode method in the System.Web assembly (requires the full .NET Framework 4 profile):
var encoded = HttpUtility.UrlPathEncode("http://zh.wikipedia.org/wiki/白雜訊");
3 Comments
According to MSDN you can't use UrlPathEncode anymore.
So, Correct way of doing it now is,
var urlString = Uri.EscapeUriString("http://zh.wikipedia.org/wiki/白雜訊");
Comments
Server.UrlEncode(s);
.NET strings are natively Unicode strings (UTF-8 encoded, to be specific) so you need to nothing more than invoke HttpServerUtility.UrlEncode (though the so-called "intrinsic" Server property will be available in most contexts in asp.net where you may want to do this).
4 Comments
I had Turkish character problem.<a href="/@Html.Raw(string)" solved the problem
1 Comment
Html.Raw() here means introducing an XSS vulnerability. There's nothing special about the Turkish-I, so using @myStringValue will work without any problems unless you're not using Unicode/UTF-8 consistently in your project and/or project files.