Introduction to basic ASP. NET objects and ASP. NET objects

Source: Internet
Author: User

Introduction to basic ASP. NET objects and ASP. NET objects

ASP. NET can become a huge software system, which has a great relationship with a large number of object class libraries. These class libraries contain many encapsulated built-in objects. developers can directly use the methods and attributes of these objects, so they can easily complete many objects with a small amount of code.
This blog will summarize several basic objects in ASP. NET. These objects are mainly divided into two major functions:
First, it is used to connect the Server and the client browser, including: Page class, Request object, Response object and Server object; second, it is used for website status management, including: Cookie object, session Object and Application object.
(1) Page class
For a page, it has its own lifecycle, mainly in the following five stages.
Page initialization: the page and its controls are initialized. The page determines whether this is a new request or a return request.
Load: if the current page is a return request, the page loads the properties of the control from the view status and control status, causing a load event.
Send back event processing: if the request is a return request, the process of sending back events of any control will be called.
Rendering: The result is sent back to the client through an HTTP response.
Detach: Clear resources used on the page and call the Unload method.
For some instances you have typed, the most common attribute is the IsPostBack attribute. This attribute is used to identify whether the Page object is in a delivery status, that is, to find out whether the request Page is the first instance or the original Page of the request.
(2) Request object
When a user sends a Request to a Web application using a Web browser on the client, the client information is sent to the Web server, and the client Request information is encapsulated as a Request object.
Common attributes of the Request object:
Request. UserAgent: used to obtain the browser version of the client.
Request. UserHostAddress: Obtain the Client IP address.
Request. ApplicaionPath: Obtain the relative path of the current website.
Request. PhysicalApplicationPath: Obtain the absolute path of the current website.
(3) Response object
The Response object encapsulates the Response of the Web server to the client request. It is used to operate information related to the HTTP protocol and return the result to the requester.
Two common methods:
Write (): output information to the browser
Redirect (): Address redirection to direct the browser to a new web page or website.
(4) Server Object
It contains some server-related information. You can obtain the latest error information, encode and decode HTML text, and access and read/write files on the server.
Common methods and attributes:
Server. MachineName: Get the Server name
Server. HtmlEncode (): encode the string in html format.
Server. Transfer (); terminate the execution of the current page and start executing the new page. Similar to Response. Redirect.
Server. MapPath (); returns the physical path.
(5) Cookie objects
This object is also seen in browsers when we access the internet. This option is usually used to clear Cookie records. We can imagine that this object is used to help us save some browsing records, and the information is stored on our client.
Using cookies has the following advantages:
No expiration rule is configured. The Cookie may expire at the end of the browser session, or may exist on the client computer indefinitely, depending on the client's expiration rules.
No server resources are required. Cookies are stored on the client and are read by the server after being sent.
Simplicity. Cookie is a text-based lightweight structure.
Data persistence. Cookie is usually the longest data retention form on the client.
(6) Session Object
It stores information for each user's session. The information in the Session object can only be used by the user, but cannot be accessed by other users on the website. Therefore, the Session object can share data between different pages, but cannot share data between different users. It is often used to determine or store user logon conditions.
Note:
The value stored in the Session object disappears when the time reaches. You can use the TimeOut attribute to set its survival time.
There can be countless Session objects on the web page. Each Session has its own independent SessionId, so there is no impact on each other.
When a user logs on, the system automatically assigns a SessionID, Which is randomly assigned and will not be duplicated. It is used to distinguish different users.
You can set the expiration time in the web. config configuration file of the website. <SessionState timeout = "30"/>
Session. Abandon (); this method is required when you log out or log out.
(7) Application Object
The Application object is also used to store information. Unlike the Session object, the information stored by the Application object can be shared among users. The value stored by the Application will not disappear until the website is closed. It is mainly used to count the number of online users, chat room speeches, and website visits. There are mainly Lock () and UnLock () methods.
Features:
The data in the object can be shared by all users in the program.
With its own events, you can be triggered as needed to execute the corresponding program code.
An Application may have multiple Application objects, which do not affect each other.
Data can be permanently stored during server running.
Using the Lock and UnLock methods ensures that multiple users cannot change their attributes at the same time.
ASP. NET's seven basic objects read the materials in other books many times, but every time after reading the documents, they do not make a summary in time, so they feel very confused, I often see but don't know what it is when writing a program. Through this summary, I sorted out the seven basic objects completely, and I finally understood a lot. When you need to use it in the future, you should at least think about what to use and what the meaning of this object is.
. Net Source: http://www.eyesourcecode.com/f/DotNet/1

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.