Introduction to ASP. NET built-in objects
1. Response
2. Request
3. Server
4. Application
5. Session
6. Cookie
The Request object mainly allows the server to obtain some data from the client browser, including parameters, cookies, and user authentication transmitted from the HTML form using the Post or GET method. Because the Request object is a member of the Page object, it can be directly used without any declaration in the program. Its class name has many HttpRequest attributes, but there are few methods, and only one BinaryRead ()
1. Use the Request. Form attribute to obtain data
Use this attribute to read the form data. 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 ")
Similarly, UserHostAddress, Browser, Cookies, ContentType, IsAuthenticatedItem, and ParamsResponse are used to output data to the client, including outputting data to the Browser, redirecting the Browser to another URL, or outputting Cookie files to the Browser. Its Class Name Is the httpResponse attribute and method.
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 ("")
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.example.com /")
3. Response. End () Terminate the running of the current page
4. Response. WriteFile (FileName) Where: FileName refers to 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 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) |
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 informationApplication ("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") updates 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. It can be used to access websites, chat rooms, and other devices.
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.
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
A Cookie is 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
- ASP. NET development tutorial
- Ten effective performance optimization methods for ASP. NET
- Analysis on ASP. NET global Exception Handling