ASP. NET advanced programming basics Article 9-Response and Server Object

Source: Internet
Author: User

Starting from this blog, we will explain how to use Response objects and Server objects. Response objects and Server objects are also widely used, such as the output information of Response objects, the location where the Server Object reads files.

  1. Response object

(1) Response Buffer input: To improve server performance, ASP. NET will not Write to the browser once by default, it will immediately output to the browser, but will buffer the data, at the right time or the end of the response will be sent to the browser together with the data in the buffer.

(2) Main Member of the Response object

1) Response. buffer, Response. bufferOutput: After Reflector decompilation, the two attributes are the same. Buffer is called BufferOutput internally. This attribute is used to control whether the response cache is used. The default value is true.

2) Response. flush () sends data in the buffer zone to the browser. It is very useful to output the written content to the browser immediately. Case: import large volumes of data, displays the number of data being imported. Use Thread. sleep simulation time.

Note: create a general processing program step. aspx and write the following code in it:

1 context. response. contentType = "text/plain"; 2 3 for (int I = 0; I <= 20; I ++) 4 5 {6 7 System. threading. thread. sleep (1000); 8 9 context. response. write ("Step" + I + "completed <br/>"); 10 11 context. response. flush (); 12 13 // if (I = 10) 14 15 // {16 17 // context. response. clear (); 18 19 //} 20 21}

3) Response. clear () clears the data in the buffer, so that the data in the buffer is cleared and not sent to the browser, in the example of outputting non-HTML data using aspx, Clear is often used to output HttpModeld.

Note: Create a New WebFrom project tupian. aspx and write the following code in it:

1 protected void Page_Load (object sender, EventArgs e) 2 3 {4 5 Response. contentType = "image/JPEG"; 6 7 Response. clear (); 8 9 string fullpath = Server. mapPath (" .jpg"); 10 11 using (System. drawing. bitmap bitmap = new System. drawing. bitmap (fullpath) 12 13 {14 15 bitmap. save (Response. outputStream, System. drawing. imaging. imageFormat. jpeg); 16 17} 18 19}

4) Response. contentEncoding: encoding of the output stream

5) content type of the Response. ContentType output stream, such as html (text/html), plain text (text/plain), and JPEG image (image/JPEG ).

6) Response. Cookie: the set of Cookies returned to the browser. You can set the Cookie through it.

7) Response. OutPutStream: output stream, which is used when outputting non-text content such as images and Excel files.

8) Response. end () terminates the response and sends the data in the buffer to the browser. The code after End () is not executed. When some illegal operations are terminated, for example, you can use End () to immediately terminate a request for leeching.

Note: add an EndTest. aspx project, drag and drop a DropDownList control and a Button control, and write the following code in Page_Load:

1 protected void Page_Load (object sender, EventArgs e) 2 3 {4 5 Response. write ("one sentence below"); 6 7 Response. end (); 8 9 Response. write ("cnblogs is my favorite, why don't you show it, haha"); 10 11}

Drag and Drop controls on the page are not displayed, only the first sentence is displayed.

9) Response. redirect (Url) redirects the browser to the new Url, which can be either redirected to the site or the site Url, Response. redirect ("http://www.baidu.com response, response.redirect(+a.html"), Redirect is to send 302 redirection to the browser, is notification "Browser" please visit the URL again, in this process, the server notifies the browser that "please visit the url again" and the browser receives a command to access the new url. Use HttpWatch to view the Http packet in the response process. Because Redirect is used by the browser to re-access the new web site, you can see the changes to the web site in the address bar, which will be used to prevent "retry" prompt when refreshing the browser ".

Note: When detecting information, can I enter it in the address bar? Q = 1 simulation.

1 protected void Page_Load (object sender, EventArgs e) 2 3 {4 5 string q = Request ["q"]; 6 7 if (q = "1 ") 8 9 {10 11 Response. write ("I am Han Yinglong"); 12 13} 14 15 else if (string. isNullOrEmpty (q) 16 17 {18 19 Response. write ("How can you be empty?"); 20 21} 22 23 else24 25 {26 27 Response. write ("congratulations"); 28 29} 30 31}

10) Response. SetCookie (HttpCookie cookie): update the Cookie written to the browser to the output stream. If the Cookie exists, it is updated. If it does not exist, it is added. It is a simplified call to Response. Cookies.

11) Response. Write () wants the browser to output the content.

12) Response. WriteFile (fileName) Outputs a file to the browser, for example: Response. WriteFile ("c:/root. ini ").

Note: Write the following code in step. ashx:

1 context. Response. ContentType = "Image/JPEG"; 2 3 string full = context. Server. MapPath (" .jpg"); 4 5 context. Response. WriteFile (full );

 

  1. Server Object

(1) Server is an attribute of context and an object of the HttpServerUtility class.

(2) Server. htmlDecode (), Server. htmlEncode (), Server. urlEncode (), Server. urlDecode () is a proxy call to the corresponding methods in the HttpUtility class. For personal recommendations, HttpUtility is always used, because it is difficult to obtain the Server Object in some places. Do not confuse HtmlEncode and UrlEncode, urlEncode processes hyperlinks and HtmlEncode processes Html code.

(3) Server. transfer (path) Internal redirection request, Server. transfer ("step. aspx ") Redirects user requests to step. aspx processing is the internal takeover of the server, and the browser cannot realize this takeover, unlike Response. redirect () experiences the process of "notifying the browser to access the url again" and "accessing the new url through commands ". Therefore, the address bar of the browser will not change. Because the browser takes over internally, you can access Redirect, Cookies, and other parameters accepted by these source pages on the redirected pages, just as these parameters are passed to him, But Redirect does not work, because it allows the browser to access it. Note that Transfer is taken over internally, therefore, it cannot be redirected to an external website like Redirect.

Note: Create a hello. aspx page and add the following code to the redirect. aspx. cs page:

Else if (q = "2 ")

{

Server. Transfer ("hello. aspx ");

}

Write the following information on the page_load page of Hello. aspx. cs:

Response. Write (Request ["q"]);

(4) Server. Transefer cannot be directly directed to ashx. Otherwise, an error will be reported "An error occurred while executing the request ".

(5) Sometimes you cannot get the HttpContext object. For example, in: Global. ascx, you can get the Current HttpContext through HttpContext. Current, and then get Response. Request. Server.

Note: Now let's talk about Response and Server. We will start to talk about Httphandler in the next blog.

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.