Response. Redirect, server. Transfer, and server. Execute

Source: Internet
Author: User

Transfer is rarely used in the past. Now it is used today. Let's take a closer look.

1. response. Redirect
The response. Redirect method causes the browser to link to a specified URL. When the response. Redirect () method is called, it creates a response, and the response header indicates the status. Code 302 (indicating that the target has changed) and the new target URL. The browser receives the response from the server and sends a request to the new URL using the information in the response header.
This means that response is used. in the Redirect method, the redirection operation occurs on the client, involving two communications with the server (two back and forth) in total: the first request to the original page, and a 302 response is obtained, the second is the new page stated in the 302 response to the request, and the page after the redirection is obtained.

2. server. Transfer
Server. transfer is a new feature in IIS 5.0. It solves two important defects of response. Redirect:
1) In response. Redirect, we cannot get any output on the first page.
2) response. redirect will lose all the attributes in the request. Of course, we can use some other methods, such as session, but some page parameters are transmitted in the request, so it will not work.
3) response. Redirect requires the client to initiate another request.
Server. Transfer solves these problems. It initiates a request directly from the server to the next page and does not require the client to send the request again. If your webpage is very dependent on response. Redirect, this small change can increase the efficiency by nearly 25%. (According to Microsoft documentation ).
The server. Transfer Method transfers the execution process from the current aspx file to another ASPX page on the same server. When server. Transfer is called, the current ASPX page is terminated and the execution process is transferred to another ASPX page. However, the new ASPX page still uses the response stream created on the previous ASPX page.
If you use the server. transfer method to implement navigation between pages, the URL in the browser will not change because the redirection is completely performed on the server side, and the browser does not know that the server has executed a page transformation.
By default, server. the transfer method does not pass form data or query strings from one page to another, but you only need to set the second parameter of this method to true, the form data and query string of the first page can be retained.
At the same time, use server. note that the target page will use the response stream created on the original page, which leads to ASP.. Net machine authentication check (MAC), The viewstate of the new page is deemed to have been tampered. Therefore, to retain the form data and query string set of the original page, you must set the enableviewstatemac attribute of the target page command to false.
Server. one disadvantage of transfer () is that when the user is in. aspx submits a form, and then uses server. transfer () enters B. aspx. If you refresh the page, the browser will ask you if you want to "retry" the form. If you click "yes", the data in the form will be resent to the server. For example, the function of sending a form is to insert a record into the database, and the result is not expected to happen-the same form is added to the database multiple times.
Server. Transfer helps reduce server requests and keep the address bar URL unchanged. It allows you to pass the query string and form variables to another page.

3. server. Execute
Server. the execute method allows the current ASPX page to execute a specified ASPX page on the same web server. When the specified ASPX page is executed, the control process returns to the original page to issue the server. the location of the execute call.
This page navigation method is similar to calling the callback function for the ASPX page. The called page can access the form data and query string set of the calling page, therefore, set the enableviewstatemac attribute of the called page command to false.

4. Differences between redirect and transfer:
● Execution Process
Redirect: browser ASP file request-> server execution-> response. Redirect statement-> the server sends the address following response. Redirect to the client browser-> the browser requests a new address
Transfer: ASP file request in the browser-> server execution-> server. Transfer statements-> server redirection to new files

● Switching objects
Redirect can switch to any existing webpage.
Transfer: Only webpages with the same directory or subdirectory can be switched.

● Data confidentiality
Redirect: The address after the jump will be the address of the page after the jump.
The address remains unchanged after server. Transfer, hiding the address of the new web page and the parameter values attached to the address. Data Confidentiality.

● Transmitted data volume (parameters attached to the URL)
Redirect: the data that can be transferred is limited to 2 kb.
Transfer: When the transmitted data exceeds 2 kb, you must use server. Transfer.

5. Differences between server. Execute and server. Transfer:
Execute transfers the execution from the current page to the specified page and returns the execution to the current page.
Transfer completely transfers the execution to the specified page.

Reference Source:

Http://www.cnblogs.com/3stones/archive/2008/03/20/1114645.html http://webservices.ctocio.com.cn/net/188/8900188.shtml

 

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.