ASP. NET contains several objects

Source: Internet
Author: User
Five objects in ASP. NET development:
For Web Applications Program When running, ASP. NET needs to collect information about the current application, user sessions, and response browser. To solve these problems, ASP. NET Contains classes for processing the information, which is used to encapsulate the context information.

In ASP. NET, these classes include httpresponse, httprequest, httpapplicationstate, httpserver, utility, and httpsessionstate. Their corresponding instance objects are response, request, application, server, session, which are defined in the page class and can be directly accessed through the Page Object.

Request object (retrieve the request information sent from the browser to the server ).

The request object is used to obtain information in the request sent from the browser to the server. When you click the submit button, the data information contained in the input control is sent to the server along with the form. The request object on the server reads the data sent through the HTTP request.

A collection of request objects is used. The Calling method is as follows.

Request. Collection ["variable"]

Collection indicates a set. Its values include form, querystring, servervariable, and cookies. The form set is used to collect the request data submitted using the POST method in the form. The data extraction must be performed on the server side. In addition, post requests must be sent using form.

The functions of the querystring set are similar to those of the form set. Used to receive data submitted using the get method. This submission method will list the data in the URL "?" Is usually referred to as "Additional URL Information ". In turn, if the URL of the requested program contains "?" in an HTTP request event, The request is followed by a data list, indicating that the request method is the get method.

The servervariable set is used to obtain environment variables, including various system information of servers and clients. The format is as follows.

Request. servervariable [parameter type]

The cookies set is used to share data in ASP. NET. The shared data is stored in the clients and Settings \ Administrator \ cookies on the client system disk. It is used to store client-related information data, such as numbers, strings, and dates. Each browser has its own cookie. It is often used for data transfer between pages of different browsers on the same computer to achieve data sharing.

In ASP, PHP, and other languages, if the client disables cookie writing, the program cannot use the cookie. However, in ASP. NET, you can write the following statements in the web. config file, which can be written under any circumstances.

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

To use cookies, you must use the reques and response objects. Cookie can be viewed as the sub-objects of these two objects. These two types of data are used: Write Data and read data. You can use the following method to write and read data in "cookie. The format for writing data is as follows.

Response. Cookies [cookie name]. value = write data;

Or as follows.

Response. Cookies [cookie index number]. value = write data;

Read the following data.

Cookiesvalue = request. Cookie ["Cookie name"]. value;

Or as follows.

Cookiesvalue = request. Cookie ["Cookie index number"]. value;

The "Cookie name" is the name of any number, String, date, and other objects. The cookie index number starts from 0 and corresponds to a cookie in the cookie set. You can also remove a cookie from a cookie in the following format.

Response. Cookies. Remove ("Name of the cookie to be removed ");

Response. Cookies. Clear ();

The former is used to remove a cookie with a specified name, and the latter is used to remove all cookies in the cookie set. (Cookie objects have lifecycles. By default, the validity period is 20 minutes. If more than 20 minutes, the written cookie will be cleared. Of course, you can also set the effective retention time of cookie objects. The method is as follows .)

Response. Cookies [cookie name]. expires = datetime;

That is, you can use the expires (expiration) attribute to set. Only the attribute value must be of the datetime type. The following program defines a cookie with an expiration time of two days.

Response. Cookies ["cookiestr"]. expires = datetime. Now. adddays (2 );

Application Object (sharing the global information of the Application)

The application object is used to indicate the status of the entire website application. These statuses share data through the Application object. All computers accessing the website application can access the data.

Storage Method of Application Data

You can use the application object to read and write data.

The format for reading data is as follows.

Data = application [variable name];

Or as follows.

Data = application [index number];

The write data format is as follows.

Application [variable name] = data;

Or as follows.

Application [index number] = data;

The "variable name" indicates the variable for storing data. The variable must be enclosed in double quotation marks. The index number starts from 0 and corresponds to a variable in the Application object. The results are the same.

You can also delete a data item in the Application object. The format is as follows.

Application. Remove ("name object ");

Application. removeat ("index of the named object ");

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

Application. removeall ();

Application. Clear ();

Because the application object is a shared object. That is to say, many computers can access the content above. This causes a problem when two or more computers operate on the objects simultaneously. The server will be faced with how to deal with this sharing relationship. We can implement non-conflict access to data through locking and unlocking.CodeAs follows.

Application. Lock ();

Int COUNT = convert. toint32 (application ["count"]. tostring ());

Application ["count"] = count + 1;

Application. Unlock ();

Session Object (maintain personal information of each customer)

After the client is connected to the server, the server generates and maintains a client session ). Simply put, the server identifies the client and gives it a number. In fact, when the web server is running, many users may simultaneously browse the website on the server. When each user (browser) establishes a connection with the Web server for the first time. The server creates a session for the user (browser ). At the same time, the server will automatically assign it a sessionid (which will be demonstrated in later programs) to mark the unique identity of this user (browser. This sessionid is a string randomly generated by the Web server consisting of 24 characters. This unique 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 and sends it to the server. After the server completes the form request, it returns the result through the user corresponding to sessionid.

Use session to store data

Like the application object, the session object is also used to share data on multiple pages. Session and application objects share data, and data types can be numbers, strings, dates, and even complex objects. At the same time, they are all stored on the server, which is different from Cookie. The difference between the two is that the former represents a browser window, while the latter represents the entire website application, and the data of the application object is used to share with all website users. As shown in, this is the scope of application and session.

Similarly, sessions can be divided into two methods: Read and Write Data.

You can use the following method to read data.

Data = session [variable name];

Alternatively, use the following method.

Data = session [index number];

The write data format is as follows.

Session [variable name] = data;

Or use the following method.

Session [index number] = data;

You can also delete a data item in the session object. You can use the following format.

Session. Remove ("name object ");

Session. removeat ("name object index ");

To remove all data from objects in the application, you can use the following format.

Session. removeall ();

Session. Clear ();

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.