Introduction to ASP. NET built-in objects

Source: Internet
Author: User
Introduction to ASP. NET built-in objects
1. Response
2. Request
3. Server
4. Application
5. Session
6. cooki

RequestObjects are mainly used by the server to obtain some data from the client browser, including parameters, cookies, and user authentication transmitted from HTML forms using post or get methods. Because the request object is a member of the page object, it can be directly used in the program without any declaration;
Its Class Name Is httprequest
There are many attributes, but there are few methods. There is only one binaryread ()
1. Use the request. Form attribute to obtain data
Use this attribute to read form data between <form> </form>. Note: Set the submission method to "Post ".
Compared with the get method, the POST method can send a large amount of data to the server.
2. Use the request. querystring attribute to obtain data
The querysting attribute of the request object can be used to obtain a set of HTTP query string variables. Through this attribute, we can read the address information http: // localhost/AAA. aspx? Uid = Tom & Pwd = ABC indicates the data marked as the red part.
Note: Set the submission method to "get"
3. Problem: request. form is used when the form submission method is post, while request. querystring is used when the form submission method is get. If an error is used, no data is obtained.
Solution: Use the request ("element name") to simplify the operation.
4. Request. servervariables ("environment variable name ")
Similar to: userhostaddress, browser, cookies, contenttype, isauthenticated
Item, Params

ResponseThe object language outputs data to the client, including outputting data to the browser, redirecting the browser to another URL, or outputting a cookie file to the browser.
Its Class Name Is httpresponse
Attributes and Methods
Write () sends string information to the client
Whether the bufferoutput attribute uses Cache
Clear () Clear Cache
Flush () forces the output of all cached data
Redirect () webpage redirection address
End () stops running the current page
Writefile () reads a file and writes it to the client output stream.
(Essence: open the file and output it to the client .)
1. response. Write variable data or string
Response. Write (variable data or string)
<% =... %>
Response. Write ("<script language = JavaScript> alert ('Welcome to ASP. net') </SCRIPT> ")
Response. Write ("<SCRIPT> window. Open ('webform2. aspx ') </SCRIPT> ")
2. The redirect method of the response object redirects the client browser to another URL to jump to another webpage.
For example:
Response. Redirect ("http://www.163.net /")
3. response. End () Terminate the running of the current page
4. response. writefile (filename)
Where:
Filename refers to the file name of the file to be output to the browser.

ServerThe object provides access to methods and properties on the server. The class name is httpserverutility.
The main attributes of the server object include:
Machinename: obtains the name of the server computer.
Scripttimeout: Get and Set Request timeout (in seconds ).
Method Name Description
Createobject creates a server instance of the COM object.
Execute to execute another ASPX page on the current server. After executing this page, return to this page to continue executing.
Htmlencode encodes the string to be displayed in the browser and returns the encoded string.
Htmldecode decodes HTML encoded strings and returns decoded strings.
Mappath returns the physical file path corresponding to the specified virtual path on the Web server.
Transfer terminates the execution of the current page and starts executing a new page for the current request.
Urlencode encodes the string representing the URL for reliable HTTP transmission from the Web server to the client through the URL.
Urldecode decodes the encoded URL string and returns the decoded string.
Urlpathencode encodes the URL part of the URL string and returns the encoded string.
Encoding:
Server. htmlencode ("HTML code ")
Decoding:
Server. htmldecode ("encoded HTML ")
1. The mappath method of the server object converts the virtual path or relative path relative to the current page to the physical file path on the Web server.
Syntax: Server. mappath ("virtual path ")
String filepath
Filepath = server. mappath ("/")
Response. Write (filepath)

ApplicationIn actual network development, objects are used to record the information of the entire network, such as the number of online users, online lists, opinion surveys, and online elections. Information is shared among multiple users in a given application and stored permanently during server running. In addition, the application object also provides methods to control access to application layer data and events that can be used to trigger the process when the application starts or stops.
1. Use the application object to save information
Use the application object to save information
Application ("key name") = Value
Or
Application ("key name", value)
Get Application Object Information
Variable name = Application ("key name ")
Or: variable name = application. Item ("key name ")
Or: variable name = application. Get ("key name ")
Update the value of the Application Object
Application. Set ("key name", value)
Delete a key
Application. Remove ("key name", value)
Delete all keys
Application. removeall ()
Or application. Clear ()
2. Multiple users may access the same application object at the same time. In this way, multiple users may modify the name object of the same application, resulting in data inconsistency.
The httpapplicationstate class provides two methods: Lock and unlock to solve the problem of access synchronization to the application object. Only one thread is allowed to access the application state variable at a time.
About locking and unlocking
Lock: application. Lock ()
Access: Application ("key name") = Value
Unlock: application. Unlock ()
Note: the lock and Unlock methods should be used in pairs.
Used by devices such as website visitors and chat rooms
3. Use application events
In ASP. net Applications can contain a special optional file-Global. asax file, also known as ASP.. NET application file, which contains the file used to respond to ASP. code of application-level events caused by the net or HTTP module.
The global. asax file provides seven events, five of which are applied to application objects.

Event Name Description
Application_start is triggered when the application starts.
Application_beginrequest is triggered at the beginning of each request
Application_authenticaterequest
Application_error is triggered when an error occurs.
Application_end is triggered when the application ends.

SessionA session refers to a user's access to a website within a period of time.
The Session Object corresponds to the httpsessionstate class in. Net, which indicates the "session state" and can save information related to the current user session.
The Session object is used to store the information required by a user to access a specific ASPX page from the moment the user leaves. When you switch the page of an application, the variables of the session object are not cleared.
For a web application, the content of the application object accessed by all users is identical, while the content of the session object accessed by different users is different. Session can save the variable. This variable can only be used by one user. That is to say, each browser has its own session object variable, that is, the session object is unique.
(1) Add new items to the session Status
Syntax format:
Session ("key name") = Value
Or
Session. Add ("key name", value)
(2) obtain the value in the session status by name
Syntax format:
Variable = SESSION ("key name ")
Or
Variable = session. Item ("key name ")
(3) deleting items in the session Status set
Syntax format:
Session. Remove ("key name ")
(4) Clear all values in the session Status
Syntax format:
Session. removeall ()
Or
Session. Clear ()
(5) cancel the current session
Syntax format:
Session. Abandon ()
(6) set the timeout period of the session Status, in minutes.
Syntax format:
Session. Timeout = Value
The global. asax file has two events applied to the session object.
Event Name Description
Session_start is triggered when the session is started.
Session_end is triggered at the end of the session

CookieIs a piece of text that the Web server saves on the user's hard disk. Cookie allows a Web site to save information on a user's computer and then retrieve it. Information fragments are stored as 'key/value' pairs.
Cookie is a text file stored on the client's hard disk. It can store information about a specific client, session, or application. It corresponds to the httpcookie class in. net.
There are two types of cookies: Session Cookie and persistent cookie. The former is temporary. Once the session state ends, it will no longer exist. The latter has a fixed expiration date, and the cookie will be stored on the user's computer as a text file before it expires.
You can use the response object to create a cookie on the server and output it to the client.
The response object supports a set named cookies. You can add cookie objects to the set to output cookies to the client.
Access the cookie through the cookie set of the request object

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.