|
There are three main ways to complete page redirection in Asp.net: The redirect method of the response object, calling the transfer or execute method of the server object.
Response. RedirectMethod causes the browser to link to a specified URL. When the response. Redirect () method is called, it creates a response, and the response header indicates the status.Code302 (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.
Server. TransferMethod to transfer 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.
Server. ExecuteThe method allows the current ASPX page to execute a specified ASPX page on the same web server. when the execution of the specified ASPX page is complete, the control process returns the location where the original page was called by server. Execute.
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.
Applicability
<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>ProgramTo 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.
|