【问题标题】:StreamReader replacing a non-breaking space with a + symbolStreamReader 用 + 符号替换不间断空格
【发布时间】:2012-11-12 19:18:54
【问题描述】:

我正在从网络表单中获取字段并尝试将输入保存到数据库中。它目前正在用“+”符号替换不间断空格(即“Test 2”=“Test+2”)正如您将在下面看到的,我有一个 hack 替换字符串中的 + 并将其替换为“ ”。

我知道这是一个短板的解决方案。我原以为 'Encoding.GetEncoding("UTF-8") 会解决这个问题。我想找到某种可以解释任何类型字符的编码。请看下面的代码:

private List<Alias> GetAliasesFromPost()
{
    List<Alias> aliases = new List<Alias>();
    using (StreamReader sr = new StreamReader(Request.InputStream, Encoding.GetEncoding("UTF-8")))
    {
        string[] postedValues = sr.ReadLine().Split('&');
        for (int i = 0; i < postedValues.Length; i++)
        {
            if (postedValues[i].StartsWith("urlAlias="))
            {
                Alias alias = new Alias();
                string active = postedValues[i - 1].Replace("active=", string.Empty);
                alias.Active = string.Compare(active, "on", true) == 0;
                alias.UrlAlias = postedValues[i].Replace("urlAlias=", string.Empty);
                alias.Notes = postedValues[i + 1].Replace("notes=", string.Empty).Replace("+", " ");
                int Id = 0;
                Int32.TryParse(postedValues[i + 2].Replace("id=", string.Empty), out Id);
                alias.Id = Id;
                aliases.Add(alias);
            }
        }
    }
    return aliases;
}

【问题讨论】:

    标签: c# asp.net streamreader


    【解决方案1】:

    要解决您提出的具体问题,请在您的 postedValues http://msdn.microsoft.com/en-us/library/system.web.httputility.urldecode.aspx 中使用 UrlDecode

    但是你真的不应该解析Request。只需使用Request.Params,值就会在那里被解析和解码。

    【讨论】:

    • 对不起,我是 StreamReaders 的新手,我添加了新的 using (StreamReader sr = new StreamReader(Request.Params); 它似乎不喜欢重载。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-23
    相关资源
    最近更新 更多