ASP. Five main objects

Source: Internet
Author: User

When the Web application is running, ASP. NET needs to gather information about current use programs, user sessions, and response browsers. To address these issues, the classes that handle this information are included in ASP. NET to encapsulate these contextual information.

Asp. NET These classes are HttpResponse, HttpRequest, httpapplicationstate,httpserver,utility,httpsessionstate and so on. Their corresponding instance objects are response,request,application,server,session, which are defined in the page class and can be accessed directly through the Page object.

Request object (retrieves the requested information sent from the browser to the server).

The request object is used to obtain information from a browser sent to the server. When the user clicks the Submit button, the data information contained in the input control is sent to the server side along with the form. The server-side request object reads data sent over HTTP requests.

The request object uses more of the collection, and its invocation method is as follows.

request.collection["Variable"]

The collection represents the collection, and its value is form,querystring,servervariable,cookies and so on. The form collection is used to collect request data submitted using the Post method in the form, and the data must be extracted on the server side. And the POST request must be sent using the form form.

The functionality of the QueryString collection is similar to the function of the form collection. Used to receive data information submitted using the Get method. This type of submission will list the data in the URL "? "is often referred to as" additional information about the URL. " In turn, if an HTTP request event is in the URL of the requested program with "? "And with a list of data behind it, it means that this request is a GET method.

The ServerVariable collection is used to obtain environment variables that contain various system information for the server and client. Its usage format is shown below.

request.servervariable[parameter Type]

The cookie collection is used in ASP. NET to share data, and its shared data is stored in documents and settings\administrator\cookies of the client system disk. Used to hold client-related information data, such as numbers, strings, dates, and so on. Each browser has its own Cookie. It is commonly used in the same computer, different browser pages between the data transfer, in order to achieve the purpose of data sharing.

In languages such as ASP, PHP, and so on, if the client disables writing cookies, it can cause the program to not use cookies. However, in ASP. NET, a statement like the following can be written in the Web. config file, which can be written in any case.

<sessionstate cookieless = "true" > </sessionState>

The use of cookies requires the use of both reques and response objects. Cookies can be considered as sub-objects of these two objects. It uses two types: write data and read data, respectively. You can use the following methods to write and read data from a "Cookie". The format for writing the data is as follows.

Response.cookies[cookie name]. Value = Write data;

Or as follows.

The index number of the Response.cookies[cookie]. Value = Write data;

Read the data below.

Cookiesvalue = request.cookie["Cookie name"]. Value;

Or as follows.

Cookiesvalue = request.cookie["Cookie index number"]. Value;

Where the "cookie name" is an arbitrary number, string, date, and other object names. The "Cookie index number", starting with 0, corresponds to a cookie in the cookie collection. Corresponding, you can also remove a cookie in the cookies, in the following format.

Response.Cookies.Remove ("The name of the cookie that needs to be removed");

Response.Cookies.Clear ();

The former is used to remove a cookie from a specified name, while the latter is used to remove all cookies from the cookie collection. (The Cookie object is life-cycle.) By default, the validity time is 20 minutes. If more than 20 minutes are written, the cookie will be erased. Of course, you can also set a valid retention time for a cookie object. The method is as follows. )

Response.cookies[cookie name]. Expires = DateTime;

This is also set by the expires (expiration) property. Just set the property value requirement to be a datetime type. The following program defines a cookie that has an expiration time of two days.

response.cookies["Cookiestr"]. Expires = DateTime.Now.AddDays (2);

Application objects (shared application global information)

The Application object is used to represent the state of the entire Web site application, which implements the sharing of data through the Application object. This data is accessible to all computers that access the site's application.

How application data is stored

The Application object enables reading and writing of data.

The format in which the data is read is as follows.

data = application[variable name];

Or as follows.

data = application[index number];

The write data format is shown below.

application[variable name] = data;

Or as follows.

application[index number] = data;

Where the "variable name" represents the variable that stores the data, the variable needs to be enclosed in double quotation marks. The index number starts at 0 and corresponds to a variable in the Application object. Their results are the same.

Corresponding, you can also delete an item of data from the Application object. Its format is as follows.

Application.remove ("named object");

Application.removeat ("Index of Named objects");

If you need to remove all of the data from the Application object, you can use the following format operation.

Application.removeall ();

Application.clear ();

Because the Application object is a shared object. In other words, many computers can access the contents of the same. This is a problem when two or more computers are working on the objects at the same time. How the server will handle this sharing relationship problem. We can use lock and unlock to achieve non-conflicting access to the data, the implementation code is as follows.

Application.Lock ();

int count = Convert.ToInt32 (application["Count"]. ToString ());

application["Count"] = count + 1;

Application.UnLock ();

Session object (maintains personal information for each customer)

When the client connects to the server, a client session is generated and maintained by the server side. In short, it is a number that the server uses to identify the client. In fact, when the WEB server is running, many users may be browsing the Web site on that server at the same time. When each user (browser) establishes a connection to this Web server for the first time. The server establishes a session for the user (the browser). At the same time, the server will automatically assign it a SessionID (which will be demonstrated in a later program) to mark the unique identity of the user (browser). This SessionID is a 24-character string randomly generated by the Web server. The only SessionID is actually very important. When a client user submits a form, the browser automatically attaches the SessionID of the user (browser) to the HTTP header information and sends it to the server side. When the server finishes processing the form request, the result is returned by the user corresponding to the SessionID.

Store data with session

As with application objects, session objects are also used to share data from multiple pages. The same point in the session as the Application object is that both are used to share data, and the data type can be numbers, strings, dates, or even complex objects. At the same time they are stored on the server side, which differs from the cookie. The difference is that the former represents a browser window, while the latter represents the entire Web site application, and the data for the Application object is used for sharing with all site users. As shown in the scope of the session and application.

Similarly, the session is divided into two methods of reading and writing data.

You can use the following methods to implement data reading.

data = session[variable name];

Or use the following method.

data = session[index number];

The write data format is shown below.

session[variable name] = data;

Or use the following method.

session[index number] = data;

Corresponding, you can also delete an item in the session object, you can use the following format operation.

Session.remove ("named object");

Session.removeat ("Index of Named objects");

To remove all data from an object in the application, you can use the following format operation.

Session.removeall ();

Session.clear ();

ASP. Five main objects

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.