NameValueCollection x = new NameValueCollection(Request.QueryString);
string x1 = x.ToString();
NameValueCollection y = HttpUtility.ParseQueryString(Request.QueryString.ToString());
string y1 = y.ToString();当我执行上面的代码时,x1和y1的值变成
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的,但我没有获得任何其他信息。
所以,我不明白为什么x和y的行为是不同的。
发布于 2017-12-20 20:18:15
如果是HttpUtility.ParseQueryString,则返回类HttpValueCollection的一个实例(source),该实例继承自NameValueCollection。显然,这个类有意义地覆盖了ToString,而不是从object继承ToString的NameValueCollection,因此只显示完整的类型名称。
在ParseQueryString中没有提到这一点,因为HttpValueCollection是internal。他们不希望你使用这个类型,你也不应该依赖于返回这个类型。
下面是ToString在HttpValueCollection中的source
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();
}https://stackoverflow.com/questions/47905788
复制相似问题