Looking at the definition type of querystring is NameValueCollection, mistakenly think that this is NameValueCollection rewrite the ToString () method, and then safely transferred the code to the business logic layer. Because the query parameters are also refactored, a namevaluecollection is rebuilt and the result of ToString () is taken for granted as the key. But the actual operation found that every time the results are the same, are the first query results. After debugging, it was found that NameValueCollection's ToString () method did not return the "System.Collections.Specialized.NameValueCollection".
First look at the debugging situation, found that the actual type of QueryString is System.Web.HttpValueCollection, the type inherits from NameObjectCollectionBase, It's the same as the base class of NameValueCollection. It is most likely that querystring directly inherits the NameValueCollection and rewrites the ToString () method.
The performance of ordinary NameValueCollection is as follows
Based on the above judgments, the basic reason why QueryString appears to be NameValueCollection but actually shows a different ToString performance.
Then continue to understand the System.Web.HttpValueCollection is sacred. This class seems convenient to use, but it also tries to use it in code, but the hint does not find the class. There is also no explicit record of the class in MSDN, which can be basically determined as a private type or protected. After searching, it is found that the class is a complete definition of System.Web.HttpValueCollection, system.web, version=2.0.0.0, Culture=neutral, publickeytoken= B03F5F7F11D50A3A, this type can be accessed or modified only by reflection. Therefore, if you want to use this kind of words, basically is more troublesome, not too worthwhile. But in the general developer's eyes, the query string and NameValueCollection can be very smooth to call, through the simple way of ToString () can be obtained. The current way to implement both conversions is as follows
Converting from query string content to NameValueCollection
Httputility.parsequerystring (querystring);(reference http://msdn.microsoft.com/zh-cn/library/ms150046 (v=vs.90). aspx), This way to get the authentic querystring,tostring () after the actual value.
Transformed from NameValueCollection to query string
You can only traverse NameValueCollection and then spell out a query string from the
Copy Code code as follows:
foreach (string key in C.keys)
{
Sb. AppendFormat ("{0}={1}", Key, C[key]);
}
But in fact we want to have a more natural way, because we tend to be willing to manipulate NameValueCollection, rather than read-only querystring.
The foreigner also put forward the opinion to this, concrete can refer to
http://msmvps.com/blogs/paulomorgado/archive/2008/07/15/ Make-the-httpvaluecollection-class-public-and-move-it-to-system-dll.aspx