Response. Redirect simply sends a message to the browser, telling the browser to locate another page. You can use the following
Code Direct the user to another page:
Response. Redirect ("webform2.aspx ")
Or
Response. Redirect ("http://www.cnnas.com /")
Server. Transfer also uses one statement to direct the user to another page, for example, server. Transfer ("webform2.aspx "). However, this statement has a series of unique advantages and disadvantages.
First, use server. transfer directs to another page to reserve server resources. By changing the server's "Focus" and transmission request, it will tell the browser to redirect, which means that you will not occupy a large number of HTTP requests, therefore, this can reduce the pressure on the server and make your server run faster.
However, please note that because "transfer" can only run between the same site on the same server end, you cannot use server. Transfer to redirect users to the site on another server. Only response. Redirect can redirect to a site other than the server.
Second, server. transfer retains the URL address of the browser. This is helpful for streamlined data input, but it also increases the debugging complexity.
Also, the server. transfer method has another parameter -- "preserveform ". If you set this parameter to true, for example, server. Transfer ("webform2.aspx", true), the query string and any form variables will be passed to the page you are located at the same time.
For example, webform1.aspx has a text box named textbox1, which is passed to webform2.aspx using preserveform to true. You can still use request. Form ("textbox1") to obtain the value of the text box.
This technology is useful for wizard-type multi-page input, but here you must note that when you use the preserveform parameter, Asp. net has a bug. Normally, an error occurs when attempting to pass the form or query string value. See: http://support.microsoft.com/default.aspx? Id = KB; en-US; q2017920
The unofficial solution is to set the enableviewstatemac attribute to true on the target page you want to pass, and then set it back to false. This indicates that you need to use the false value of enableviewstatemac to solve this problem.
Conclusion: Response. Redirect simply tells the browser to access another page. Server. Transfer helps reduce server requests, keep the address bar URL unchanged, and allow you to pass the query string and form variables to another page (with a small defect ).
Important: do not confuse the server. transfer and server. execute, server. execute Executes a page and returns the result. execute is very useful, but in ASP.. net, it is replaced by the fresher method, so ignore server. execute.
Use httpcontext. rewritepath to work with server. Transfer/execute
I think many people know how to use httpcontext. rewritepath to change httphandler in applicaton_start and other events.
If you use rewritepath before calling server. Transfer/execute, you can specify querystring.
For example
Context. rewritepath ("AA. aspx", "", "okok = 3333 ");
Context. server. Transfer ("test2.aspx ");
In this way, test2.aspx can be executed, and querystring is okok = 3333.
This is especially useful. I currently use this method to avoid using response. Redirect.
Differences between server. Transfer and response. Redirect
Server. Transfer (ASP 3.0 or above) and response. Redirect existed in the previous ASP. Page. navigate is a new function provided by ASP. NET Beta 1. The difference between them is:
1. server. transfer-transfer the control of processing from one page to another. During the transfer process, the information stored by the internal control (such as request and session) remains unchanged without leaving the server, therefore, you can jump from page a to page B without losing the user submission information collected in page. In addition, the URL bar of the browser remains unchanged during the transfer process.
2. response. Redirect-sends an HTTP Response to the client, telling the client to jump to a new page, and then the client sends a jump request to the server. When this method is used, all internal control data cannot be saved. When page a jumps to page B, page B cannot access the data submitted by form in page.
3. Page. navigate-page. functions implemented by navigate and response. redirect is similar. It actually involves three steps: Call response first. redirect, Uninstall all controls in sequence, and call response. end.
Note. in net beta 2, Microsoft will no longer include page. the navigate function should be used now. redirect to replace page. navigate to save time for future upgrades.
(1) server. transfer Method:
server. transfer ("m2.aspx"); // page redirection (executed on the server ).
the server stops parsing this page, saves the data on this page, and then redirects the page to m2.aspx.
the result of adding the m2.aspx page to the browser is returned.
(2) server. execute method:
server. execute ("m2.aspx");
after the server saves the data on this page and redirects the page to m2.aspx for execution,
return to this page to continue execution. merge the three results and return them to the browser.
All of the above are server-side page redirection, so there is no page change record in the browser (the displayed address will not change ).
therefore, if you refresh this page, some other unexpected events may occur.
this type of page orientation allows you to complete other functions, such as accessing the server control on the previous page.
(3) response. redirect:
when the browser requests An ASPX page, it encounters the Redirect (URL) method.
It is equivalent to telling the browser that you need to access a page first, the browser then sends a request to the server to access this page.
the relocation is performed through the browser, and an additional round-trip process is generated between the server and the browser.
when the network condition is not good, the two requests greatly reduce the response speed of the
application Program , it even occupies excessive bandwidth.
conclusion:
when the network status is good, the Redirect (URL) method is the most efficient !!
the server. Transfer Method and server. Execute method are the most flexible !!
the server. Execute method occupies the most resources.