asp.net page turn to Response.Redirect, Server.Transfer, Server.Execute difference _ practical Tips

Source: Internet
Author: User
Tags httpcontext
Response.Redirect simply sends a message to the browser, telling the browser to navigate to another page. You can use the following code to direct the user to another page:
Response.Redirect ("webform2.aspx")
Or
Response.Redirect ("http://www.cnnas.com/")
Server.Transfer also directs the user to another page through a statement, such as: Server.Transfer ("webform2.aspx"). However, this statement has a number of unique advantages and disadvantages.
First, by Server.Transfer booting to another page to retain server resources, by changing server-side "focus" and transfer requests instead of telling browser redirection, which means you won't be consuming more HTTP requests, this can reduce server pressure and make your server run faster.
Note, however, that because "transfer" can only run between the same site on the same server side, you cannot use Server.Transfer to redirect users to a site on another server. To redirect to a site other than the server, only Response.Redirect can do it.
Second, Server.Transfer retains the URL address of the browser end. This is helpful for streamlined data entry, but it also increases the complexity of debugging.
Also: The Server.Transfer method also has another parameter--"Preserveform". If you set this argument to true, such as: Server.Transfer ("webform2.aspx", True), then query string and any form variables are also passed to the page you are targeting.
For example: WebForm1.aspx has a text box named TextBox1, and you use Preserveform to pass to webform2.aspx, you can still use Request.Form ("TextBox1") to get the value of the text box.
This technique is useful for a wizard-style multiple-page input, but here's one thing you should be aware of is that when you use the Preserveform parameter, ASP.net has a bug, and typically an error occurs when you try to pass a form or query string value. See also: http://support.microsoft.com/default.aspx?id=kb;en-us; Q316920
The unofficial solution is to set the enableViewStateMac property to True in the destination page you want to pass, and then set it back to False. This means that you need to use the False value of enableViewStateMac to solve this problem.
Summary: Response.Redirect simply tells the browser to access another page. Server.Transfer helps reduce server requests and keeps address bar URLs unchanged, allowing you to pass query string and form variables to another page (with a slight flaw).
Important: Do not confuse Server.Transfer and Server.execute,server.execute to execute a page and return the result, in the past Server.Execute useful, but in asp.net, it was fresher Method instead, the Server.Execute is ignored.

Use Httpcontext.rewritepath to match Server.transfer/execute

I think a lot of people know how to use Httpcontext.rewritepath to change HttpHandler in Applicaton_start and other events.

If you use RewritePath before Server.transfer/execute calls, you can also specify the QueryString effect.

For example

Context.rewritepath ("AA.") Aspx "," "," okok=3333 ");
Context.Server.Transfer ("test2.aspx");

In this way, the test2.aspx can be executed, and QueryString is okok=3333.

This use is particularly large. I currently use this method to avoid using Response.Redirect.

The difference between the Server.transfer,response.redirect

The difference between Server.Transfer (ASP 3.0) and Response.Redirect in the previous ASP, Page.navigate is the new functionality offered by the ASP.net Beta 1:

1, Server.Transfer-Used to transfer control of processing from one page to another page, in the process of transfer, did not leave the server, internal controls (such as: request, session, etc.) of the saved information unchanged, so you can jump from page A to page B You do not lose the user submissions that are collected in page A. In addition, the browser's URL bar does not change during the transfer process.

2, Response.Redirect-sends an HTTP response to the client, tells the client to jump to a new page, the client sends a jump request to the server again. When you use this method, you will not be able to save all the internal control data, page a jumps to page B, and page B will not be able to access the data submitted by Form A in page a.

3, page.navigate-page.navigate implementation of the function and Response.Redirect almost, it actually includes three steps: First call Response.Redirect, followed by unloading all the controls, the last Call Response.End.

Notably, Microsoft will no longer include the Page.navigate feature in. NET Beta 2, and you should now replace Page.navigate with Response.Redirect to save time for future upgrades.


(1) Server.Transfer method:
Server.Transfer ("m2.aspx");//page turn (performed on server).
The server stops parsing this page, saves the data before it is turned, and then shifts the page to m2.aspx,
and returns the data before the turn to the browser, plus the m2.aspx page results.


(2) Server.Execute method:
Server.Execute ("m2.aspx");
After the server saves the data before the page is turned, the page moves to m2.aspx execution,
and then returns to this page to continue. The three results are merged and returned to the browser.

These are server-side page shifts so browsers do not have page change records (the displayed address does not change).
Therefore, if the user refreshes this page, there may be some other unexpected condition.
This kind of page steering can accomplish other functions, such as accessing the server-side controls on the previous page.


(3) Response.Redirect:
When the browser requests an ASPX page, it encounters the Redirect (URL) method, and
is equivalent to telling the browser that you first need to visit a page to is the browser to send a request to the server to this page.
Relocation is performed through the browser, resulting in additional round trips between the server and the browser.
When network conditions are not good, two requests can be significant
to reduce the application's response speed, or even to occupy extra bandwidth.

Summary:
Redirect (URL) method is the most efficient when the network is in good condition!!
Server.Transfer method and Server.Execute method are most flexible!! The
Server.Execute method consumes the most resources.
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.