Asp.net six built-in objects (2)

Source: Internet
Author: User

4. Application Object
The Application object is a collection object that is available throughout the ASP. NET Website. Different users can access the variables of the Application object at different times. Because it is a built-in object, it is similar to the Response object and Request object. It can be directly used without instantiating any object.
The Application object is used to store variables or objects so that when the webpage is accessed again (whether it is the same connector or visitor ), the stored variables or object content can also be re-called for use. That is to say, the Application is public for the same website and can be shared among users.
(1) attributes and methods of the Application Object
N Count: gets the number of Application object variables.
N Get: Get the value of a variable using the index or variable name.
N GetKey: use an index to obtain the value of an object variable.
N Add (name, value): adds an Application object variable named name and value.
N Set (name, value): update the value of the Application object variable named name.
N Clear: Clear all Application object variables.
N Remove (name): Delete the Application object variable named name.
N Lock: Lock all Application object variables.
N UnLock: UnLock all Application object variables.
(2) important events of the Application Object
N OnStart event: when the first client requests an ASP. NET file, ASP. NET calls and executes the event;
N OnEnd event: when the server is shut down, ASP. NET call execution, usually write all the information that needs to be permanently saved to the disk, so that the server can be reloaded when it is restarted and accessed.
Pay attention to the following points when using the Application object to access variables:
N Application object variables should be frequently used data. If they are only occasionally used, you can store information in disk files or databases.
N Application object is a collection object, which can store objects in addition to text information.
5. Session Object
ASP. NET provides a built-in object Session to store, read, and change Session information of a specific user. Each Session object is unique for each user, which is mainly reflected in:
1) for a user's access, the Session object is unique during the access period and can share information between pages through the Session object. As long as the Session does not time out or the Abandon method is not called, the information in the Session will not be lost. Session objects cannot share information between users, while Application objects can share information between different users.
2) each time a user accesses a Session, data cannot be shared between two accesses.
(1) attributes and methods of the Session Object
N SessionID: indicates the existence of a Session. When a user establishes a connection with the server, the server automatically assigns a SessionID to the user, which can distinguish sessions ).
N TimeOut: indicates the maximum time (in minutes) that a user will wait for no activity before the end of a session.
N Abandon: Call this method to remove a user's Session object and release the resources it occupies. The Session_OnEnd event is triggered when the Abandon method is called.
N Add (name, value): adds a Session variable named name and value.
N Clear: Clear all Session variables.
N RemoveAll: Clear all Session variables.
N Remove (name): clears the Session variable named name.
N RemoveAt (index): deletes the Session variable whose index is index.
N CopyTo: copy the set of current session Status values to a one-dimensional array.
(2) Two events of the Session Object
N Session_OnStart event: triggered when a Session is created;
N Session_OnEnd event: the event is called at the end of the Session (probably because the Session times out or the Abandon method is called.
N 6. Cookie object
 
In Web programming, it indicates a common text file with a length of no more than 4 K. This file is stored on your hard disk and can be accessed by a Web browser.
There are two types of cookies: Session Cookie and permanent Cookie.
The n session Cookie is temporary and stored in the user's memory instead of the hard disk. It only exists when the browser opens. Once the session ends or times out, the Cookie is deleted.
N permanent cookies are permanently stored on the customer's hard disk and can be used until the specified expiration date.
Therefore, to save Cookie information for a long time, you need to set the Cookie expiration date so that the Cookie can become a permanent Cookie.
(1) attributes and methods of Cookie objects
N Expires: gets or sets the Cookie expiration date and time. The default value is 1000 minutes.
N Name: Get or set the Cookie Name.
N Value: gets or sets a single Cookie Value.
N Values: obtains the set of key-value pairs contained in a single Cookie object.
N Add: Add a name and value pair to the Cookie.
(2) practical methods of Cookie
Cookies do not belong to the Page object. They are used differently from Application and Session objects.
1) create a Cookie
HttpCookie object name = new HttpCookie (Cookie name );
2) Add name and value pairs to cookies
Cookie object name. values. Add (name, value );
3) write the Cookie to the client
Response. AppendCookie (Cookie Object Name );
4) Obtain Cookie objects
HttpCookie Cookie object name = Request. Cookies ["Cookie name"];
5) obtain the value in the Cookie object
Cookie. values ["keyword name"];
(3) set the Cookie expiration time
The Expires attribute of the Cookie object is used to obtain or set the expiration date and time of the Cookie object. The expiration date and time are subject to the client's computer.
For example:
Set the expiration time:
MyCookie. Expires = Convert. ToDateTime ("2011-09-09 ");
Setting never expires:
MyCookie. Expires = DateTime. MaxValue;
Author: "He Junfeng blog"

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.