Seconds understand built-in objects in ASP.

Source: Internet
Author: User
Tags allkeys cookie names

On the blog, the small part of the main simple introduction of the control in ASP., this blog, the small part of the main simple summary of the built-in objects in ASP. Seven built-in objects are: Request, Response, application, Cookies, Session, Server, Trace. These objects make it easier for users to collect information sent through a browser request, the corresponding browser, and store user information for other specific state management and page information delivery, first, let's look at the following picture:

Next, the small series begins with an overview, basic syntax, common properties and methods, and a few small examples, detailing seven built-in objects.

Response object:

Overview: Inherits from the System.Web.HttpRespone class, which is used to send information to the client and to control the sending process. For example, dynamically create Web page display content in the browser, change the HTTP header header, redirect the client to the specified page, set buffer information, etc.

Basic syntax: Response [. Property | method] [variable]; The properties and methods can only select one of the two parameters. A variable is a string variable that is used as a parameter to a method.

Common Properties and methods:

For example: Jump to the New interface: (1), Response.Redirect ("http://www.baidu.com");
(2), Response.Redirect ("default1.aspx");
In the page output, specify the string: Response.Write ("Use of the Write Method!") ")。

PS: Here, small add a small point of knowledge, response and request the difference, please click on the details.

Request object:
Overview: The Request object is an instance of the System.Web.HttpRequest class that is used to obtain information submitted and uploaded from the client. Use this object to access any information that is passed in an HTTP request, including information that the user submits in the Web form, parameter information in the URL, client information such as the client IP address, the client browser version information, the DNS name of the client machine, and so on, including the binary data sent to the server.
Basic syntax:
Rquest[.collect| Property | method [variable], where the collection, property, Method 3 parameters can only select one, or 3 do not. A variable is a string type that specifies the item to retrieve, or as a parameter to a method, for example:

A, request.querysring["id"] means to get the ID parameter value after the URL; 135

B, request["id"] also means to get the ID parameter value, but the ASP. NET will traverse Querysring, Form, cookie and other data collection to retrieve this parameter, it is recommended to specify the name of the data collection, to provide efficiency;
C, Request.TotalBytes represents the size of the data received from the customer segment, in bytes
Common Properties and methods:

Continuation table:

For example:
A, get the information passed: Response.Redirect ("Default2.aspx?username=" +textbox1.text.trim ());//Pass the user name information
String str=request.querystring["UserName"]. ToString ();//Get User name information
b, get the browser information: Reponse.write ("browser is:" +requst.browser.type "+" <br/> ");
Reponse.write ("The name of the browser is:" +requset.browser.browser+ "<br/>");

PS: When submitting data to the server, it is divided into two ways

Get commit:

<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" >//get              String userName = request.querystring["txtUserName"]. ToString ();              String userpwd = request.querystring["Txtuserpwd"]. ToString ();              Response.Write ("Login username" + "userName");  </span></span>

Post submission:

<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" >//post              String userName = request["txtUserName"]. ToString ();              String userpwd = request["Txtuserpwd"]. ToString ();              String userName = Request.Form.Get ("txtUserName"). ToString ();              String userpwd = Request.Form.Get ("Txtuserpwd"). ToString ();  </span></span>
Get PK POST:

         a, get the data in the form as Variable=value, Add to the URL that the action points to, and both use "?" connections, and each variable is connected by a "&" connection; Post is the data in the form that is placed in the data body of the forms, in the same way that the variables and values are passed to the URL that the action points to.   For example, you have a page index.html, this page has a text field, the name is: A, the value is 123, you use get commit, your URL will become: index.html?a=123, in the acquisition, is to go back from the browser parameters, not the form!
         b, get is not secure, because in the transfer process, the data is placed in the requested URL, and now many existing servers, proxy servers, or user agents will log the request URL to the journal file, Then place it somewhere so that there may be some privacy information that is visible to third parties. In addition, users can see the submitted data directly in the browser, and some system internal messages will be displayed in front of the user. All the actions of the post are not visible to the user.  
         c, get transmits a small amount of data, mainly because of the URL length limit, and post can transfer a large amount of data, so the upload file can only use post.  
         d, get is the default method for form. For more details, please click here.

Server object:

Overview: Theserver object inherits from the System.Web.HttpServerUtility class, which is used to configure the server environment, create COM objects and scripting components, provide access to the server's interfaces, colleagues it can also convert data formats, manage site pages Functions such as the execution of polygons. Popularly said that the server object is the bottom-level butler of the ASP.

Common Properties and methods:

For example, we write a blog and space have access to, the following examples of this statistical access to the count of small examples, the code is as follows:

<span style= "FONT-SIZE:18PX;" >private void Page_Load (object sender, System.EventArgs e)   {   //place user code here to initialize page   application.lock ();   application["counter"] =   Convert.ToInt32 (application["Counter"]) +1;   Application.UnLock ();   Response.Write ("<center><b> counter Example </b><br>");   Response.Write ("  welcome  !")  you are the first "   +application[" counter "]. ToString () + "bit visitor </center>");   }  </span>
    

Session object:

Overview:TheSession object is used to store a specific page from the moment a user starts to access a particular home, until the user leaves.

The information that is required for the user session. When a user switches between pages of an application, the variables in the session object are not cleared and the class name is HttpSessionState.

Common Properties and methods:

Continuation table:

For example: Use Session object to record user information:

<span style= "FONT-SIZE:18PX;" >private void Btnlogin_click (object sender, System.EventArgs e)   {   session["name"]=txtname.text;   session["Password"]=txtpwd.text;   Response.Redirect ("test.aspx");   }     The code in the Test.cs Page_Load event is as follows:    private void Page_Load (object sender, System.EventArgs e)   {   lblmessage.text= ' Congratulations to you! "+session[" name "]. ToString () + ", you have the note   book successful, the password is" +session["password"]. ToString ();   }  </span>

Cookie Object :

Overview: A cookie is a small piece of text that is stored by the browser on the client system (hard disk) and is a marker. The Web server is embedded in the user's browser to identify the user and is sent to the Web server with each user request. Cookies are similar to sessions and application, and are also used to hold relevant information, but the biggest difference between cookies and other objects is that the cookie stores the information on the client side, and the session and application are saved on the server. This means that whenever a user connects to a server, the Web site can access cookie information. In this way, it is convenient for users to use, but also convenient for users of the site management.

Common Properties and methods:

For example: All cookies sent through the client, and the name, expiration date, security parameters, and values of each cookie are sent to the HTTP output:

<span style= "FONT-SIZE:18PX;" >int Loop1, LOOP2;  HttpCookieCollection Mycookiecoll;  HttpCookie MyCookie;  Mycookiecoll = Request.Cookies;  Put all the cookie names into a character array  string[] arr1 = Mycookiecoll.allkeys;  Gets a single Cookie object for  (loop1 = 0; Loop1 < arr1 with a cookie name. Length; loop1++)  {     MyCookie = Mycookiecoll[arr1[loop1]];     Response.Write ("Cookie:" + mycookie.name + "<br>");     Response.Write ("Expires:" + mycookie.expires + "<br>");     Response.Write ("Secure:" + mycookie.secure + "<br>");  Place the value of a single cookie into an array of objects     string[] arr2 = MyCookie.Values.AllKeys;  Traverse the cookie value collection to print all values for      (loop2 = 0; loop2 < arr2. Length; loop2++)     {        Response.Write ("Value" + Loop2 + ":" + arr2[loop2] + "<br>");     }  </span>
     

Cache object:

Overview: An instance of the class is created for each application domain, and the instance remains in effect as long as the corresponding application domain remains active. Information about such instances is provided through the cache property of the HttpContext object or the Cache property of the Page object.

Common Properties and methods:

For example:

A, retrieve the value of the TextBox control cache:

Cache.get ("Mytextbox.value");

B. Delete the cache for Key the related objects:

<span style= "FONT-SIZE:18PX;" >public Voidremoveitemfromcache (Object sender, EventArgs e) {   if (cache["Key1"]! = null)     cache.remove ("Key1 ");} </span>
To sum up, for the basic usage of the built-in objects of the ASP, a deeper step requires more practice.

Small message: the blog post, Small part of the main simple introduction of the built-in objects in ASP, each object small series mainly from the overview, basic properties and methods, and some small examples of three aspects of the built-in objects in ASP, the first contact with the built-in objects in ASP, the small part of the understanding is not very deep, for the built-in object of ASP. The basic usage, but also need us in the actual operation of a lot of practice, b/s study, not to be continued ...

Seconds understand built-in objects in ASP.

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.