Details: request and request. Params

Source: Internet
Author: User
Tags allkeys servervariables

In ASP. NET programming, there are three common data sources from the client: querystring, form, and cookie. We can access these three objects in httprequest. For example, we can get some parameters contained in the URL from querystring and get the form data imported by the user from form, you can obtain session status and other user-defined parameter information from cookies. In addition to these three objects, httprequest also provides servervariables for us to obtain some Web Server variables. Generally, the four data sources are clear and I don't think anyone will confuse them.

In general, if we can know in advance which set a parameter comes from, then directly accessing that set will make the problem simple. However, the more common data sources are usually querystring and form, and especially when jquery's $. when Ajax () is used, parameters can be stored in querystring or form at will. Therefore, the server can flexibly cope with this situation, you can use request [] and request. params. The story of this article also arises: What is the difference between request [] and request. Params ??

  Review [my core Asp.net objects]

Due to some introductions and examples of the [Asp.net core objects in my mind], some people may not see the text due to various reasons, so I have to post it again here:

These two attributes allow us to easily search for the querystring, form, cookies, or servervariables set based on a key. Generally, if a request is sent using the get method, we generally access querystring to obtain user data. If the request is submitted using the POST method, we generally use form to access the form data submitted by users. The use of Params and item makes it unnecessary to distinguish between get and post when writing code. The only difference between these two attributes is that item accesses these four sets in sequence and returns the result if it is found, while Params accesses, first, the data of the four sets is merged into a new set (created when the set does not exist), and then the specified result is searched.

To better demonstrate the differences, see the following sample code:

<Body>
<P> item result: = This. itemvalue %> </P>
<P> Params result: = This. paramsvalue %> </P>

<HR/>

<Form action = "<% = request. rawurl %>" method = "Post">
<Input type = "text" name = "name" value = "123"/>
<Input type = "Submit" value = "Submit"/>
</Form>
</Body> Public partial class showitem: system. Web. UI. Page
{
Protected string itemvalue;
Protected string paramsvalue;

Protected void page_load (Object sender, eventargs E)
{
String [] allkeys = request. querystring. allkeys;
If (allkeys. Length = 0)
Response. Redirect ("showitem. aspx? Name = ABC ", true );


Itemvalue = request ["name"];
Paramsvalue = request. Params ["name"];
}
}

Browser display before submission:

Click Submit to display the browser:

The difference is obvious, and I will not talk about it anymore. Let's talk about my suggestion: Try not to use Params. It is not only the judgment problem caused by the above results, but it is not necessary to create another set. What's worse is that after the cookie is written, the set is also updated.

As I mentioned above, the objective reason is that the blog has more valuable objects to introduce, so it does not take too much space to introduce these two sets. Next, let's take a closer look at their differences.

Implementation Analysis

In the previous example, I demonstrated that different results are obtained when accessing request [] and request. Params. Why are there different results? Let's take a look at Microsoft's implementation in. NET Framework first.

First, let's take a look at the implementation of request [], which is a default indexer. The implementation code is as follows:

Public String This [String key]
{
Get
{
String STR = This. querystring [Key];
If (STR! = NULL ){
Return STR;
}
STR = This. Form [Key];
If (STR! = NULL ){
Return STR;
}
Httpcookie cookie = This. Cookies [Key];
If (cookie! = NULL ){
Return cookie. value;
}
STR = This. servervariables [Key];
If (STR! = NULL ){
Return STR;
}
Return NULL;
}
}

This Code indicates that the querystring, form, cookies, and servervariables collections are accessed Based on the specified key. If any collection is found, the system returns immediately.

The implementation of request. Params [] is as follows:

Public namevaluecollection Params
{
Get
{
// If (httpruntime. hasaspnethostingpermission (aspnethostingpermissionlevel. Low ))
//{
// Return this. getparams ();
//}
// Return this. getparamswithdemand ();

// For ease of understanding, I commented out the above Code. The key is the following call.
Return this. getparams ();
}
}
Private namevaluecollection getparams ()
{
If (this. _ Params = NULL ){
This. _ Params = new httpvaluecollection (0x40 );
This. fillinparamscollection ();
This. _ Params. makereadonly ();
}
Return this. _ Params;
}
Private void fillinparamscollection ()
{
This. _ Params. Add (this. querystring );
This. _ Params. Add (this. form );
This. _ Params. Add (this. Cookies );
This. _ Params. Add (this. servervariables );
}

The implementation method is as follows: first judge whether the field member _ Params is null. If yes, create a set and set querystring, form, cookies, all data in the four sets of servervariables is filled in, and subsequent queries are directly carried out in this set.

We can see that this is two completely different implementation methods. This is because, in some special cases, the access results will be different.

The difference is that request. Params [] creates a new set and merges the four data sources. If a key with the same name is encountered, the natural result will be different.

  Cookie again

In my blog [My Asp.net core object], I talked about request. params [], I simply said: and worse, the set will be updated after the cookie is written. How can we understand this sentence?

I want to see how we write a cookie and send it to the client. Next, let's copy the original article in [coookie:

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.