Discussion on ASP.net webpage redirection (posting)

Source: Internet
Author: User
Part of the Content reproduced from: http://blog.csdn.net/zb219/archive/2006/02/03/591305.aspx
There are three methods for page redirection:
1. response. Redirect ("url ");
2. server. Transfer ("url ");
3. server. excute ("url ")
Server. Transfer ("url") has three advantages compared with the most common response. redirdbms ("url ").

Advantages:
1. It is directly redirected on the server side and does not need to communicate with the browser before redirecting like response. Redirect, which is highly efficient.
2. It can pass the submission value of the previous page. For example, if the value of page a is submitted to page B, and the value of page B is transferred to Page C, Page C can also receive the value submitted by page.
3. You can hide URLs.

Disadvantages:)
You cannot refresh the page. For example, If logon information is submitted to page B on page A and transfer is sent to page a after page B is processed, page a cannot be refreshed, and the specified page does not expire. If pages A and B are not in the same directory, we will find more unexpected results. The images and hyperlinks with relative links have all changed, this is because page B reads the content of page a but outputs it as page B, so the path is changed.

There are four page navigation methods in Asp.net. How do I select one for your page?

· If you want users to decide when to convert a page and which page to go to, hyperlink is the most suitable.

· If you want to use a program to control the conversion target, but the conversion time is determined by the user, use the hyperlink control of the Web server to dynamically set its navigateurl attribute.

· If you want to connect a user to resources on another server, use response. Redirect.

· Use response. Redirect to connect users to non-aspx resources, such as HTML pages.

· If you want to retain the query string as part of the URL, use response. Redirect.

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

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

· To ensure that the HTML output is valid, use response. Redirect instead of server. Transfer or server. Execute.

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

Comparison of three methods provided by the jump page Asp.net

Today, the instructor talked about three ways to jump to a page. The following is a summary:

1 response. the redirect method does not jump quickly because it takes two rounds (two PostBack), but it can jump to any page, there is no website page restriction (that is, you can jump from Yahoo to Sina), and you cannot skip logon protection. However, slow speed is the biggest defect! Redirect jump mechanism: First, send an HTTP request to the client. The notification needs to jump to a new page, and then the client sends a jump request to the server. It should be noted that after the jump, all data information stored in the internal space will be lost, so session is required.

2 server. Transfer is fast, only one PostBack is required, .... It must be on the same site because it is a server method. In addition, he can skip logon protection. You can try to write a small program: design a jump from page 1 to page 2, but to enter page 2, You need to log on and perform form authentication. However, if the jump statement uses transfer, the logon page will not pop up. The redirection request of this method occurs on the server, so the URL address of the browser still retains the address of the original page!

3 sever.exe cute this method is mainly used in the page design, and he must jump to the page under the same site. This method needs to be used to insert the output results of a page to another ASPX page. Most of them are in the table, where a page is nested to another page.

Summary:

Use redirect to redirect a user to a page on another server.

When you need to redirect a user to a non-ASPX page, for example, HTML uses redirect

When the query string needs to be retained to the server as part of the URL, because the other two methods cannot achieve two PostBack operations, the data is first brought back to the server using Redirect

Switch between aspx pages (logon not involved) Using Transfer

Execute is used to insert the output result of the ASPX page to another ASPX page.

Of course, I forgot another hyperlink! Of course, you don't need to talk too much about this. He uses hyperlinks when the party needs users to decide when to jump to the page.

By the way, how to use the Redirect method to use Chinese Characters in query strings is often garbled because URLs do not support Chinese characters. In this case, the conversion is required:

String message = server. urlencode ("welcome to the racing column ");

Convert the query string first.

Response. Redirect ("webform2.aspx? MSG = "+ message );

First, response. Redirect ("A. aspx"). After saving the data on this page, the server directly redirects the page to A. aspx. This method has a defect, that is, all the Request Parameters on this page will be lost after turning, and this method requires the client to initiate a request.

A new function server. Transfer ("A. aspx") is introduced in IIS 5.0, which solves the problem of losing the request parameter of this page after turning. Because it initiates a request directly from the server to the next page, the client does not need to send the request again. It corresponds to response. the difference between redirect is: response. redirect can be switched to any page, while server. transfer can only switch to a webpage in the same directory or subdirectory; response. when redirect is switched, The address will change to the page address after the jump, while the server. the original address remains unchanged when the transfer is switched, and the passed parameter value is also hidden.

Another method is server. Execute (A. aspx), which is similar to server. Transfer. The main difference is that after server.exe cute is switched to a. aspx, the original page will be returned for further processing.

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.