"ASP. NET base" ASP. NET built-in objects

Source: Internet
Author: User

To be precise, there is no built-in object for ASP. This says that JSP actually put the request, response these as JSP's built-in object, here is only to borrow a bit of JSP's argument.
The Web server that is central in the web is used to process HTTP requests from clients. Because HTTP is a stateless protocol, that is, it does not remember the last time who requested it, does not actively ask the client, only when the client unsolicited request, the server will respond. 1. "Request"Request encapsulates the client solicitation information. The common properties of request are as follows:
Property name Value type Description
Applicationpath String Gets the root path of the requested resource on the Web site
ContentEncoding Encoding Set the encoding of the request object
Cookies HttpCookieCollection A collection of cookies sent to the server by the client
QueryString NameValueCollection Collection of query strings for the current request
Urlreferrer Url Gets the URL to which the user jumps to the current page
2. "Response"Response represents the server response object. Each time a client makes a request, the server uses a response object to process the request, and after processing the request, the server destroys the corresponding object so that it can continue to accept other customer service requests. Response Common properties are as follows:
Property name Value type Description
Charset String Represents the character set used by the output stream
ContentEncoding Encoding Set the encoding of the output stream
ContentLength Int The byte size of the output stream
ContentType String HTTP MIME type for output stream
Cookies HttpCookieCollection A collection of cookies sent to the client by the server
Output TextWriter Character output stream for server response object
Redirectlocation String REDIRECT the current request
Response Common methods
Property name return value type Description
AppendCookie void Adds a cookie to the cookie collection of the Response object
Clear void Clears all content output from the buffer
Close void Close the current server-to-client connection
End void Terminates the response and sends the output from the buffer to the client
Redirect void REDIRECT Current request
3. "Server"The server object is the object that is used to obtain information about the server. It is commonly used in the following ways:
Property name return value type Description
Execute void Executes the specified resource, and then executes the code on this page after execution
HtmlDecode String Eliminate the effects on special string encodings
HtmlEncode String Encode a special string
MapPath String Gets the physical path of the specified relative path on the server
Transfer void Stops executing the current program and executes the specified resource
UrlDecode String Decoding a path string
UrlEncode String Encode a path string
4. "Session"The session object is used to hold information related to a particular user, the data in the session is saved on the server side, the session is created when the client needs it, and the session is destroyed when the client is not needed, so that it no longer occupies server memory. The default lifetime of the session in ASP. 20 minutes, that is, when we set a session in 9:00, if the client does not have any requests before 9:20, then its life cycle will be 9:20 minutes end. But once the user sends a request to the server in 9:19, the session now has a lifetime of 20 minutes based on the current time, which means that the session's life cycle ends at 9:39. The session has the following characteristics: The data in the session is saved on the server side, the session can save any type of data, the session default life cycle is 20 minutes, you can manually set a longer or shorter time. Suppose we want to set a session to save the user name, the session name is "UserName", the value is "sa", the code is as follows:
session["UserName"]= "sa";
There must be more than one place in a website where the session is used, so when setting up and getting the session, it is manipulated by the name of the session, and the session is set to store any type of object (that is, type objects), so the session must be retrieved according to A forced type conversion of the actual type at the time of the setting (if, of course, a data type such as Int/byte/short is stored in the session, getting the value of the session is a unboxing rather than a forced type conversion), for the above session, get The code for the Session value is as follows:
string username= (string) session["username"];
For the above code, there is a problem to note: when the corresponding session is not set or the session is destroyed because it exceeds the life cycle, the above code may throw an exception. We can first determine whether there is a specified name of the session, if it does not exist, do not have to obtain, only if the existence of the value of the session, the above code can be improved as follows:
Stringif(session["UserName"]!=null//  Gets the value of the specified session when the session with the specified name is present  username= (string) session["  UserName"];}

5. "Cookie"The cookie object and the session object are also used to save specific user-related data, but the session is different from the cookie is stored on the client and not on the server, each time the client makes a request, the cookie is sent to the server, The server re-sends the Cookie to the client each time it responds to a client request. The cookie holds the following characteristics: The data in the cookie is stored on the client, the cookie can only hold data of the string type, if other types of data need to be saved in the cookie, it needs to be converted to a string type, and the cookie has its default life cycle, can also be set manually, maximum can be set to expire after 50 years. As in the case of the Session, there may be more than one cookie used in one site, and we still differentiate between cookies by the name of the cookie. The process of setting up a cookie is to add a cookie to the cookie collection of the server's response object Response, and the Response object sends all the cookies in the cookie collection to the client. The code is as follows (still save the user name as an example):
New HttpCookie ("UserName""sa"); RESPONSE.COOKIES.ADD (cookie);

Obtaining a cookie is a cookie that finds its name from the client's request object and, of course, a situation where the cookie does not exist, so it is also necessary to check for the existence of a cookie of the specified name before obtaining it, as follows:
Stringif (request.cookies["UserName"null) {      = request.cookies["UserName"]. Value; }

6. "Application"

Application and Session store the same data type and storage location, is the Object type of data (that is, any type), and stored on the server, the data in different application can be set by all users of the site or get. And there is no time limit to the data stored in the application, unless we delete it manually or the server restarts, the stored data will be lost.
Here are the differences between Session, Cookie, and application:
Name Scope of Use Storage location Storage data type Life cycle
Session Specific user Server Object, which is any type Yes, you can set it yourself
Cookies Specific user Client String, which is also the value string You can set it yourself
Application All Users Server Object, which is any type No
7. "<%%> Expressions"<%%> part of the code used to write the program. In which variables and methods can be declared. As follows:
 <%     stringname=request.form["UserName"].     Trim (); stringUserName; if(request.cookies["UserName"] != NULL) {UserName=request.cookies["UserName"].     Value; }      %>

In <%%> is code that complies with C # requirements.

8. "<%=%> Expressions"<%=%> is the value used to make a variable in the output stream. Its usage is as follows:
<% int  =6 %> <% =%>
Response.Write () output and <%=%> output The final effect is the same. Source: zhoufoxcn.blog.51cto.com/792419/166803

"ASP. NET base" ASP. NET built-in objects

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.