Convert string to uri in its simple form .NET
I want to convert for example:
Aión to URI string, so using System.Uri.EscapeDataString
this translates as Ai%C开发者_JS百科3%B3n but I expect Ai%F3n
How can I translate characters, the way I want?
I'm using a regular winform application not a ASP page
Server.HTMLEncode
This would give you
"Aión"
Thanks to @Paul McCowat and the last asnwer on that link I came up with a function that does what I want:
public static string ConvertToUri(string uri_string)
{
StringBuilder Encoded = new StringBuilder();
foreach (char Ch in uri_string)
{
if (Uri.EscapeUriString(Ch.ToString()) != Ch.ToString())
{
Encoded.Append("%");
Encoded.AppendFormat("{0:x2}", Encoding.Unicode.GetBytes(Ch.ToString())[0]);
}
else
{
Encoded.Append(Ch);
}
}
return Encoded.ToString();
}
精彩评论