I am working on an asp.net MVC web application. and i am building a URI to be sent to a web api. but the UriBuilder
is adding these characters %u200b
to the beginning of a parameter.
here is my method:-
public string Add(string title, string account,string site,string description)
{
XmlDocument doc = new XmlDocument();
using (var client = new WebClient())
{
var query = HttpUtility.ParseQueryString(string.Empty);
query["account"] = account;
query["site"] = site;
query["title"] = title;
query["description"] = description;
string apiurl = System.Web.Configuration.WebConfigurationManager.AppSettings["ApiURL"];
var url = new UriBuilder(apiurl);
url.Query = query.ToString();
string xml = client.DownloadString(url.ToString());
doc.LoadXml(xml);
now the site
parameter will be passed to the method as Manchester (MAN)
but the final query will have the parameter with %u200b
added to it as follow:-
https://****?account=ABC&site=%u200bManchester+(MAN)&title=ABCDE
so can anyone advice on this please? why the UriBuilder
is adding %u200b
to the parameter ?? now the value i am passing is actually a drop-down option, and it is rendered correctly as follow + if i chose another option for the site name i will not face the problem:-
The issue is that you have a zero width space in your string (which, as the name suggests, is 'invisible').
Unfortunately string.Trim
does not remove those characters, as per the docs:
Notes to Callers: The .NET Framework 3.5 SP1 and earlier versions maintain an internal list of white-space characters that this method trims. Starting with the .NET Framework 4, the method trims all Unicode white-space characters (that is, characters that produce a true return value when they are passed to the Char.IsWhiteSpace method). Because of this change, the Trim method in the .NET Framework 3.5 SP1 and earlier versions removes two characters, ZERO WIDTH SPACE (U+200B) and ZERO WIDTH NO-BREAK SPACE (U+FEFF), that the Trim method in the .NET Framework 4and later versions does not remove. In addition, the Trim method in the .NET Framework 3.5 SP1 and earlier versions does not trim three Unicode white-space characters: MONGOLIAN VOWEL SEPARATOR (U+180E), NARROW NO-BREAK SPACE (U+202F), and MEDIUM MATHEMATICAL SPACE (U+205F).
Thus you need to either move to .NET 3.5 SP1 or earlier (not recommended) or use string.Replace("\u200B", "")
as @StephenMuecke suggested.
Or, even better, fix your source database to remove the errant character there.
I'd also recommend installing Notepad++ to more easily see these hidden characters in future.