Learning: What are the differences between page Jump Methods redirect, transfer, and execute?

Source: Internet
Author: User

 

 

1 response. the redirect method does not jump quickly because it takes two rounds (two PostBack), but it can jump to any page, there is no website page restriction (that is, you can jump from Yahoo to Sina), and you cannot skip logon protection. However, slow speed is the biggest defect! Redirect jump mechanism: First, send an HTTP request to the client. The notification needs to jump to a new page, and then the client sends a jump request to the server. It should be noted that after the jump, all data information stored in the internal space will be lost, so session is required.

2 server. Transfer is fast, only one PostBack is required, .... It must be on the same site because it is a server method. In addition, he can skip logon protection. You can write a smallProgramTry: design a jump from page 1 to page 2, but login and form authentication are required to enter page 2. However, if the jump statement uses transfer, the logon page will not pop up. The redirection request of this method occurs on the server, so the URL address of the browser still retains the address of the original page!

3 sever.exe cute this method is mainly used in the page design, and he must jump to the page under the same site. This method needs to be used to insert the output results of a page to another ASPX page. Most of them are in the table, where a page is nested to another page.

Summary:
When you need to redirect a user to a page on another server, use redirect
When you need to redirect a user to a non-ASPX page, such as HTML, use redirect
When the query string needs to be retained to the server as part of the URL, because the other two methods cannot achieve two PostBack operations, the data is first brought back to the server, and the Redirect
Switch between aspx pages (logon not involved), use transfer
Execute is used to insert the output result of the ASPX page to another ASPX page.
Of course, I forgot another hyperlink! Of course, you don't need to talk too much about this. He uses hyperlinks when a user needs to decide when to jump to a page.

By the way, how to use the Redirect method to use Chinese Characters in query strings is often garbled because URLs do not support Chinese characters. In this case, the conversion is required:
String message = server. urlencode ("welcome to the racing column ");
First convert and then use the query string
Response. Redirect ("webform2.aspx? MSG = "+ message );

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/2000killer/archive/2007/11/26/1902636.aspx

 

In ASP. NET applications, there are multiple ways to navigate between web forms: Using Hyperlink, using response. Redirect, using server. Transfer, or using server. Execute. This article analyzes the similarities and differences between the four navigation methods and their advantages and disadvantages to help you select the best navigation method.

1. hyperlink

The simplest way to enter another form from a form is to use the HTML hyperlink control. In a web form, use the hyperlink html Code Class:

<A href = "webform2.aspx"> enter Form 2 </a>
When the user clicks this hyperlink, webform2.aspx executes and sends the result to the browser. The hyperlink navigation method can be used almost anywhere, including HTML pages and common ASP pages. ASP. NET also provides another alternative method, namely, the hyperlink Server Control:
<Form ID = "form1" method = "Post" runat = "server">
<Asp: hyperlink id = "hyperlink1" runat = "server"
Navigateurl = "webform2.aspx"> go to Form 2 </ASP: hyperlink>
</Form>
The preceding HTML code runs the same result as the first example, because ASP. Net regards the hyperlink web server control as an HTML hyperlink control. But there is one important difference between the two. hyperlink web server controls can be programmed on the server side. Specifically, you can change the navigateurl attribute in the program code to allow the construction of a hyperlink that can dynamically change according to the current state of the application. For example:

Private sub button#click (_
Byval sender as system. Object ,_
Byval e as system. eventargs )_
Handles button1.click
Hyperlink1.navigateurl = "webform3.aspx"
End sub
After the code is executed, if the user clicks the link, the user will see webform3.aspx instead of webform2.aspx.

2. Use a program to control redirection

Although hyperlink can navigate from one page to another, this navigation method is completely controlled by the user. Sometimes, we may use code to control the entire navigation process, including when to go to another page. In these cases, ASP. NET has three different methods to achieve similar goals: to call the Redirect method of the response object, to call the transfer or execute method of the server object. The behavior of these three navigation methods is similar, but there are also differences.

2.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.2 server. Transfer

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.

2.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.

By default, the output of the called page is appended to the current response stream. However, server. the execute method has an overloaded method that allows a textwriter object (or its sub-object, such as the stringwriter object) to obtain the output of the called page, rather than directly appending it to the output stream, on the original page, you can easily adjust the location of the output result of the called page.

To illustrate the working process, we will create a web form, place a button control (button1) and a text control (literal1), transfer it to the Code view on the design interface, and add a system. the imports statement of the IO namespace, and then add the code executed when the user clicks the button:

Private sub button#click (_
Byval sender as system. Object ,_
Byval e as system. eventargs )_
Handles button1.click
Dim SW as stringwriter = new stringwriter ()
Server. Execute ("webform2.aspx", SW)
Literal1.text = Sw. tostring ()
End sub
Create the second webform2.aspx page for the same web application. Go to the HTML view of the page and modify its page command to disable viewstate check:

<% @ Page Language = "VB" autoeventwireup = "false" codebehind = "webform2.aspx. VB"
Inherits = "navigate. webform2" enableviewstatemac = "false" %>

Go to the design view and add some controls for the second page. Next, set the first page to the default page to start the application. Click the button to display the webform2 control where the literal button is placed in webform1. 1. Note that the page title and URL still display the original page webform1.

Figure 1: page for merging two source files with server. Execute

Use server. transfer or server. when using the execute method to implement navigation, note that the final page may not be a legal HTML page, because the page that is finally returned to the client may contain multiple <HTML> and <body> tags. Internet Explorer seems to be able to tolerate and handle such cases correctly, but if you want to use other browsers, you 'd better test it carefully.

Iii. Comparison and Selection

Since there are so many ways to navigate from one page to another, how should we select the best navigation method? The following are some considerations:

If you want users to decide when to convert a page and which page to go to, hyperlink is the most suitable.

If you want to use a program to control the conversion target, but the conversion time is determined by the user, use the hyperlink control of the Web server to dynamically set its navigateurl attribute.

If you want to connect a user to resources on another server, use response. Redirect.

Use response. Redirect to connect users to non-aspx resources, such as HTML pages.

If you want to retain the query string as part of the URL, use response. Redirect.

If you want to transfer the execution process to another ASPX page of the same web server, you should use server. transfer instead of response. redirect, because server. transfer can avoid unnecessary network communication to achieve better performance and browsing performance.

If you want to capture the output results of An ASPX page and insert the results to a specific location of another ASPX page, use server. Execute.

To ensure that the HTML output is valid, use response. Redirect instead of server. Transfer or server. Execute.

 

ArticleSource: http://www.cnblogs.com/luren/articles/614731.html

 

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.