Introduction to Common ASP.NET Built-in Objects

There are 6 common ASP.NET built-in objects in total: 1. Response 2. Request 3. Server 4. Application 5. Session 6. Cookie Microsoft explains Page.Response like this: gets the HttpResponse object associated with the Page object. This object lets you send HTTP response data to the client and contains information about the response. Put more simply, dumbed down: all actions the server sends to the client are in Response, such as adding things to the page, redirecting the client. The Request object mainly lets the server obtain some data from the client browser, including parameters passed by Post or GET methods from HTML forms, Cookies, and user authentication. Because the Request object is a member of the Page object, no declaration is needed in the program to use it directly; its class name is HttpRequest, with many properties but few methods — only one, BinaryRead(). Microsoft’s technical documentation: http://msdn.microsoft.com/zh-cn/library/vstudio/microsoft.managementconsole.internal.request(v=vs.90).aspx The Server object provides access to methods and properties on the server. Its class name is HttpServerUtility. MachineName: gets the computer name of the server. ScriptTimeout: gets and sets the request timeout (in seconds). Method name Description CreateObject Creates a server instance of a COM object. Execute Executes another aspx page on the current server, then returns to this page to continue execution after the page finishes. HtmlEncode HTML-encodes a string to be displayed in the browser and returns the encoded string. HtmlDecode Decodes an HTML-encoded string and returns the decoded string. MapPath Returns the physical file path corresponding to the specified virtual path on the Web server. Transfer Terminates execution of the current page and begins execution of a new page for the current request. UrlEncode Encodes a string representing a URL for reliable HTTP transmission from the Web server to the client over the URL. UrlDecode Decodes an encoded URL string and returns the decoded string. UrlPathEncode URL-encodes the path portion of a URL string and returns the encoded string. Encoding: Server.HtmlEncode(“HTML code”) Decoding: Server.HtmlDecode(“encoded HTML”) The Application object’s purpose in actual web development is to record information about the entire network, such as the number of people online, the online list, opinion polls, and online elections. It shares information among all users of a given application and persists data during the server’s runtime. Moreover, the Application object has methods to control access to application-layer data and events that can trigger procedures when the application starts and stops. 1. Using 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 Application object value Application.Set(“key name”, value) Remove a key Application.Remove(“key name”, value) Remove all keys Application.RemoveAll() or Application.Clear() 2. There may be multiple users accessing the same Application object at the same time. This can cause multiple users to modify the same Application named object, leading to data inconsistency. The HttpApplicationState class provides two methods, Lock and Unlock, to solve the access synchronization problem for the Application object, allowing only one thread to access the application state variables at a time. About locking and unlocking Lock: Application.Lock() Access: Application[“key name”] = value Unlock: Application.Unlock() Note: Lock and UnLock methods should be used in pairs. Can be used for website visitor counts, chat rooms, etc. 3. Using Application events An ASP.NET application can contain a special optional file — the Global.asax file, also called the ASP.NET application file, which contains code that responds to application-level events raised by ASP.NET or HTTP modules. The Global.asax file provides 7 events, 5 of which apply to the Application object. Event name Description Application_St…