首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么NameValueCollection的行为不同?

为什么NameValueCollection的行为不同?
EN

Stack Overflow用户
提问于 2017-12-20 20:10:29
回答 1查看 151关注 0票数 2
代码语言:javascript
复制
NameValueCollection x = new NameValueCollection(Request.QueryString);
string x1 = x.ToString();
NameValueCollection y = HttpUtility.ParseQueryString(Request.QueryString.ToString());
string y1 = y.ToString();

当我执行上面的代码时,x1和y1的值变成

代码语言:javascript
复制
x1="System.Collections.Specialized.NameValueCollection"
y1="abc=1&xyz=2"   //url pass to controller is 'localhost/first/getresult/?abc=1&xyz=2'

我不明白为什么x1 and y1 is different的价值。我查看了ParseQueryString()的文档,它显示它是return NameValueCollection的,但我没有获得任何其他信息。

所以,我不明白为什么xy的行为是不同的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-20 20:18:15

如果是HttpUtility.ParseQueryString,则返回类HttpValueCollection的一个实例(source),该实例继承自NameValueCollection。显然,这个类有意义地覆盖了ToString,而不是从object继承ToStringNameValueCollection,因此只显示完整的类型名称。

ParseQueryString中没有提到这一点,因为HttpValueCollectioninternal。他们不希望你使用这个类型,你也不应该依赖于返回这个类型。

下面是ToStringHttpValueCollection中的source

代码语言:javascript
复制
public override String ToString() {
    return ToString(true);
}

internal virtual String ToString(bool urlencoded) {
    return ToString(urlencoded, null);
}

internal virtual String ToString(bool urlencoded, IDictionary excludeKeys) {
    int n = Count;
    if (n == 0)
        return String.Empty;

    StringBuilder s = new StringBuilder();
    String key, keyPrefix, item;
    bool ignoreViewStateKeys = (excludeKeys != null && excludeKeys[Page.ViewStateFieldPrefixID] != null);

    for (int i = 0; i < n; i++) {
        key = GetKey(i);

        // Review: improve this... Special case hack for __VIEWSTATE#
        if (ignoreViewStateKeys && key != null && key.StartsWith(Page.ViewStateFieldPrefixID, StringComparison.Ordinal)) continue;
        if (excludeKeys != null && key != null && excludeKeys[key] != null)
            continue;
        if (urlencoded)
            key = UrlEncodeForToString(key);
        keyPrefix = (key != null) ? (key + "=") : String.Empty;

        string[] values = GetValues(i);

        if (s.Length > 0)
            s.Append('&');

        if (values == null || values.Length == 0) {
            s.Append(keyPrefix);
        }
        else if (values.Length == 1) {
            s.Append(keyPrefix);
            item = values[0];
            if (urlencoded)
                item = UrlEncodeForToString(item);
            s.Append(item);
        }
        else {
            for (int j = 0; j < values.Length; j++) {
                if (j > 0)
                    s.Append('&');
                s.Append(keyPrefix);
                item = values[j];
                if (urlencoded)
                    item = UrlEncodeForToString(item);
                s.Append(item);
            }
        }
    }

    return s.ToString();
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47905788

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档