Summary of six built-in objects in ASP. NET

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

the request object mainly allows the server to obtain some data from the client browser, including parameters, cookies, and user authentication transmitted from HTML forms using the 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 few methods exist. There is only one binaryread ().
1. use request. use the form attribute to obtain data
to read form data between

. 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 request. querystring attribute:
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. question: 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 request ("element name") to simplify the operation.
4. request. servervariables ("environment variable name")
similar: userhostaddress, browser, cookies, contenttype, isauthenticated
item, Params

the response 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 () Send string information to the client
whether the bufferoutput attribute uses cache
clear () clear cache
flush () force output of All cached data
redirect () webpage redirection address
end () terminate the current page
writefile () read a file and write 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 (" ")
response. write (" ")
2. the redirect method of the response object redirects the client browser to another URL to jump to another webpage.
example:
response. redirect ("http://www.163.net/")
3. response. end () Terminate the running of the current page
4. response. writefile (filename)
where:
filename indicates the name of the file to be output to the browser

the Server Object provides access to methods and properties on the server. the class name is httpserverutility.
the server object has the following attributes:
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 another ASPX page on the current server, after this page is executed, return to this page and continue to execute
htmlencode to HTML encode the string to be displayed in the browser and return 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 executes 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)

The purpose of an Application Object in network development is 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 is started
application_beginrequest is triggered when each request starts
application_authenticaterequest is triggered when the user is authenticated
application_error is triggered when an error occurs.
application_end is triggered when the application ends.

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 stores 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 state.
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) delete an item in the session state set
Syntax:
session. remove ("key name")
(4) Clear all values in the session state
syntax format:
session. removeall ()
or
session. clear ()
(5) cancel the current session
Syntax:
session. abandon ()
(6) sets the timeout period of the session state, in minutes.
syntax format:
session. timeout = value
global. the asax file contains two events applied to the Session object.
event Name Description
session_start is triggered when the session starts.
session_end is triggered when the session ends.

cookie is a piece of text stored on the user's hard disk by the Web server. 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.
A 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.
two types of cookies are available: 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 the cookie 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.