The difference between Request.QueryString and general NameValueCollection _ practical skills

Source: Internet
Author: User
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.