The difference between Server. Transfer and Response. Redirect in page navigation, response. redirect

Source: Internet
Author: User

The difference between Server. Transfer and Response. Redirect in page navigation, response. redirect

1. Server. Transfer

This API transfers the control of processing from one page to another. The information stored by the internal control (such as request and session) of the transfer project remains unchanged. Therefore, you can jump from page a to page B without losing the information on page.

2. Response. Redirect

Send an HTTP Response to the client browser, telling the client which page to jump to, and the client sends a jump request to the server. Using this method, the data of the internal control cannot be saved. Page a jumps to page B, and page B cannot access the data submitted by form in.

The comparison is as follows:

Server. Transfer redirects the user to another page using the Server method, Response. Redirect redirects the user from the browser to another page,

The advantage of server. Transfer is to pass the page parameters to the specified page. The server only transmits the context to another page. You can share the upstream and downstream information between pages without using a large number of http requests temporarily. This reduces the number of requests sent from the client to the server, reduces the pressure on the server, and enables your server to run faster, response. redirect requires an additional round-trip process, which affects performance.

After Server. TransFer jumps to the page, the url address displayed by the browser does not change, and the user's browser does not know that the url is being transferred. Therefore, historical records are not updated. If the user renews the page, unexpected results are generated.

Note that Server. Transfer can only be accessed from the same site on the same Server. It cannot be redirected to. asp or. asmx pages. Only Response. Redirect can be used.

When programming, we try to use Server. Transfer when redirecting inside the website system, and Response. Redirect can be used for targeting external addresses.

 

Reprinted below:

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. 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 do some other
Method, such as session. However, some page parameters are passed in the request. In this case, it won't 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.

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.

Server. Execute, effective only when the WEB Server uses IIS5.0 or a later version. Its usage is: Server. Execute ("URL ")

Function:Stop executing the current page and transfer the execution control to the new page specified by the URL. Your current environment (session status and any current transaction status) will be passed to the new page, after the new page is executed, the control will return to the original page and continue executing the statement after the Execute method in the original page.

The function of this method is a bit similar to a process call, but it calls a complete ASP page instead of a process.

The called ASP page is equivalent to a part of the called page.

 

For example:To access different pages based on different client languages, the implementation code is:

<%
Language = Request. ServerVariables ("HTTP_ACCEPT_LANGUAGE ")

If language = "zh-cn" then

Server. Execute ("chinese/index. asp ")

Else
Server. Execute ("english/index. asp ")
End if
%>

A good feature of Server. Execute is the ability of running conditions. Generally, inclusion cannot be conditional, because the # include Directive is pre-processed before the script code executes the processing conditions. However, you can use Server. Execute to write such code, view a session variable, and decide which part of the code to call, instead of always loading the two parts of the Code.
If Session ("DoThisOrThat") = "this" Then
Server. Execute ("this. asp ")
Else
Server. Execute ("that. asp ")
End If

4. Differences between Server. Execute ("another. aspx") and Server. Transfer ("another. aspx:
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

Summary:
When the network status is good, the Redirect (url) method is the most efficient !! Can be redirected to aspx or non-aspx (html) resources on the same or non-Same server
The Server. Transfer Method and Server. Execute method are the most flexible !! But it can only be transferred to the same Application directory, which may lead to unexpected results.
The Server. Execute method occupies the most resources.

Server. Execute can solve the problem of dynamic include inclusion.

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.