Server.Transfer and Response.Redirect differences

Source: Internet
Author: User

Fundamentally, the response is to call the browser to re-turn to the specified Web page, and server naturally occurs on the server side of the main, so there will be the following differences:

1. Server.Transfer can only jump to the page specified in the local virtual directory, while the Response.Redirect is very flexible;
2. Server.Transfer can easily transfer the page parameters to the specified page;
3. When using, Server.Transfer jumps to other pages, the browser display address will not change, sometimes it will cause misunderstanding, of course, some occasions need such effect;
4. Server.Transfer can reduce the client's request to the server;

5, Server.Transfer can only be a specific page, you can not add URL parameters. However, the in-page variables can remain passed.
6. What does the URL parameter mean?
Server.Transfer ("Xxx.aspx?mode=???"); Yes, I can.



Asp. The comparison and selection of net four page navigation methods
In an ASP. NET application, there are several ways to navigate between Web Forms: with hyperlinks, with Response.Redirect, with Server.Transfer, or with Server.Execute. This article will analyze the similarities and differences between these four navigation methods and their pros and cons to help you choose the best way to navigate.

First, Super link

The simplest way to enter another form from one form is to use an HTML hyperlink control. In Web Forms, the HTML code classes that use hyperlinks are as follows:

<a href= "webform2.aspx" > Entry Form 2</a>

When the user clicks the hyperlink, Webform2.aspx executes and sends the result to the browser. Hyperlink navigation can be used almost anywhere, including HTML pages and normal ASP pages. Asp. NET also provides another alternative way to use the Hyperlink server control:

<form id= "Form1" method= "POST" runat= "Server" >
<asp:hyperlink id= "HyperLink1" runat= "Server"
Navigateurl= "webform2.aspx" > Entry Form 2</asp:hyperlink>
</form>



The HTML code above runs the same result as the first example, because ASP. Hyperlink Web server control is treated as an HTML hyperlink control. But there is a significant difference between the two, HyperLink Web server controls can be programmed on the server side. Specifically, you can change its NavigateUrl property in your program code, allowing you to construct hyperlinks that target specific targets that can be dynamically changed depending on the current state of the application, such as:

Private Sub Button1_Click (_
ByVal sender as System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
Hyperlink1.navigateurl = "Webform3.aspx"
End Sub



After this code executes, if the user clicks on the link, he will see the webform3.aspx instead of the webform2.aspx.

Second, the use of programmatic redirection

Although hyperlinks can be navigated from one page to another, this navigation is entirely user-controlled. There are times when we might want to use code to control the entire navigation process, including when to go to another page. In these cases, ASP. NET has three different ways to achieve similar purposes: Call the redirect method of the Response object, invoke the transfer or Execute method of the server object. The behavior of these three navigation modes is basically similar, but there are 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 an answer that indicates the status Code 302 (indicating that the target has changed) and the new destination URL. The browser receives the reply from the server and uses the information in the reply header to make a request to the new URL.

Say When using the Response.Redirect method, the redirect operation occurs on the client, which involves a total of two communications with the server (two back and forth): the first is a request to the original page, a 302 response is received, and the second is a new page declared in the request 302 answer, which is the page after the redirect.

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 terminates execution and the execution process goes to another ASPX page, but the new ASPX page still uses the reply stream created by the previous ASPX page.

If you use the Server.Transfer method to navigate between pages, the URL in the browser does not change, because the redirect is done entirely on the server side, and the browser does not know that the server has performed a page transformation.

By default, the Server.Transfer method does not pass the form data or query string from one page to another, but the form data and query string for the first page can be preserved as long as the second parameter of the method is set to true.

At the same time, it should be noted when using Server.Transfer: The target page will use the reply stream created by the original page, which results in the machine validation check (machines authentication check) of the ASP. MAC) thinks the viewstate of the new page has been tampered with. Therefore, if you want to preserve the form data and query string collections of the original page, you must set the enableViewStateMac property of the page directive of the target pages to false.

2.3 Server.Execute

The Server.Execute method allows the current ASPX page to execute a specified ASPX page on the same Web server, and when the specified ASPX page finishes executing, the control flow returns to the original page where the Server.Execute call was made.

This page navigation is similar to a function call for an ASPX page, where the called page has access to the form data and query string collection that made the calling page, so the enableViewStateMac property of the page directive of the invoked pages is set to False.

By default, the output of the called page is appended to the current reply stream. But The Server.Execute method has an overloaded method that allows the output of the called page to be fetched through a TextWriter object (or its sub-object, such as a StringWriter object), rather than being appended directly to the output stream, so that the location of the called page output can be easily adjusted in the original page.

To illustrate how it works, let's create a Web form, put a button control (Button1), and a text control (LITERAL1) into the code view in the design interface, Add an Imports statement for the System.IO namespace and then add the code that executes when the user taps the button:

Private Sub Button1_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



Then create a second page webform2.aspx for the same web app. Go to the HTML view of the page and modify its page directive to prohibit viewstate checking:

<%@ page language= "vb" autoeventwireup= "false" codebehind= "WebForm2.aspx.vb"
inherits= "Navigate.webform2" enableviewstatemac= "false"%>



Then go to Design view and add a few controls to the second page. Next, set the first page as the default page and launch the app. Click the button, the WebForm2 control will show where the literal button is placed in WebForm1, one, note that the page title and URL still show the original page WebForm1.

Figure one: Merging two source files pages with Server.Execute

When navigating with the Server.Transfer or Server.Execute method, it is also important to note that the resulting page may not be a legitimate HTML page because the page that is ultimately returned to the client may contain multiple <HTML> and <body Marks such as >. IE browsers seem to tolerate and correctly handle such situations, but if users want to use other browsers, it's best to test them carefully.

<b> comparison and selection </b>

Since there are so many ways to navigate from one page to another, how do you choose the best way to navigate? Here are a few things to consider:

<b>·</b> If you want to let users decide when to convert a page and which page to go to, the hyperlink works best.

<b>·</b> If you want to use a program to control the target of the transformation, but the timing of the conversion is determined by the user, use the Web server's hyperlink control to dynamically set its NavigateUrl property.

<b>·</b> If you want to connect a user to a resource on another server, use Response.Redirect.

<b>·</b> uses Response.Redirect to connect users to non-ASPX resources, such as HTML pages.

<b>·</b> If you want to keep 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 on the same Web server, You should use Server.Transfer instead of Response.Redirect, because Server.Transfer can avoid unnecessary network traffic for better performance and browsing results.

<b>·</b> If you want to capture the output of an ASPX page and then insert the result into a specific location on another ASPX page, use Server.Execute.

<b>·</b> if you want to make sure that the HTML output is legitimate, use Response.Redirect, and do not use the Server.Transfer or Server.Execute methods.


Server.Transfer and Response.Redirect differences

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.