Comparison and Selection of ASP. NET page navigation methods

Source: Internet
Author: User
I checked some documents and found that the difference between the two is quite obvious. Basically, response calls the browser to redirect to the specified webpage, and the server naturally occurs on the server side, so there are the following differences:

1. server. Transfer can only jump to the page specified by the local virtual directory, while response. Redirect is very flexible;
2. server. Transfer can easily pass the page parameters to the specified page;
3. When server. Transfer jumps to another page during use, the address displayed by the browser will not change, and sometimes it will lead to misunderstanding. Of course, this effect is also required in some occasions;
4. server. Transfer can reduce client requests to the server;

5 \ Server. Transfer can only be a specific page, and URL parameters cannot be added. However, variables in the page can be passed.
6. What does URL parameter mean?
Server. Transfer ("XXX. aspx? Mode = ??? "); Yes

 

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 web forms, HTML code classes that use hyperlinks are as follows:

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

<B> 3. Comparison and Selection </B>

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:

<B> · </B> if you want the user to decide when to convert the page and which page to go to, the hyperlink is the most suitable.

<B> · </B> if you want to use a program to control the conversion target, but the conversion time is determined by the user, use the web server's hyperlink control to dynamically set its navigateurl attribute.

<B> · </B> if you want to connect a user to resources on another server, use response. Redirect.

<B> · </B> use response. Redirect to connect users to non-aspx resources, such as HTML pages.

<B> · </B> if you want to retain the query string as part of the URL, use response. Redirect.

<B> · </B> if you want to transfer the execution process to another ASPX page of the same web server, use server. transfer instead of response. redirect, because server. transfer can avoid unnecessary network communication to achieve better performance and browsing performance.

<B> · </B> if you want to capture the output result of An ASPX page and insert the result to a specific location of another ASPX page, use server. Execute.

<B> · </B> to ensure that the HTML output is valid, use response. Redirect instead of server. Transfer or server. Execute.

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.