使用 UriBuilder 构建我的 URI 已将一个奇怪的字符 %u200b 添加到其中一个参数

Building my URI using UriBuilder has added a weird charecters %u200b to one of the parameters

我正在开发 asp.net MVC 网络应用程序。我正在构建一个要发送到网络的 URI api。但是 UriBuilder 将这些字符 %u200b 添加到参数的开头。 这是我的方法:-

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);

现在 site 参数将作为 Manchester (MAN) 传递给方法,但最终查询将添加带有 %u200b 的参数,如下所示:-

https://****?account=ABC&site=%u200bManchester+(MAN)&title=ABCDE

所以有人可以就此提出建议吗?为什么 UriBuilder%u200b 添加到参数 ?? 现在我传递的值实际上是一个下拉选项,如果我选择另一个,它会正确呈现如下 +站点名称的选项我不会遇到问题:-

问题是您的字符串中有一个 zero width space(顾名思义,它是 'invisible')。

不幸的是 string.Trim 没有删除那些字符,根据 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).

因此您需要移动到 .NET 3.5 SP1 或更早版本(不推荐)或按照@StephenMuecke 的建议使用string.Replace("\u200B", "")

或者,更好的是,修复源数据库以删除那里的错误字符。

我还建议安装 Notepad++ 以便将来更轻松地查看这些隐藏的字符。