You will often want to redirect users to other pages as part of your Web application. ASP.NET provides the following ways for you to build redirection into your Web pages:
-
Using hyperlinks on pages.
-
Configuring cross-page posting, which enables you to specify an alternate target page when the current page is submitted.
-
Redirecting programmatically by forcing the browser to request a different page.
-
Redirecting programmatically by transferring control to a different page in the same Web application.
Each of these options is described below. A table at the end of the topic summarizes the options and provides guidelines to help you decide when to use each option.
Hyperlinks
You can use an HTML anchor tag (<a>
) on an ASP.NET Web page to create static links, or you can programmatically control the link text and target URL of hyperlinks by using the
Cross-Page Posting
By default, buttons in an ASP.NET Web page post the page to itself. Cross-page posting enables you to configure a button on an ASP.NET Web page to post the current page to a different page. A typical example is when creating a multi-page form. You can configure buttons on the page to move to the next and previous pages of the form.
Cross-page posting is similar to hyperlinks in that the transfer is initiated by a user action. However, in cross-page posting, the target page is invoked using an HTTP POST command, which sends the values of controls on the source page to the target page. In addition, if the source and target page are in the same Web application, the target page can access public properties of the source page. As always, all of the pages in the application can share information stored in session state or application state.
For more information, see Cross-Page Posting in ASP.NET Web Pages and How to: Post ASP.NET Web Pages to a Different Page.
Redirecting Programmatically Using the Browser
You can programmatically redirect (that is, force the browser to a new page without user intervention) by calling the
Redirecting Programmatically on the Server
You can also redirect programmatically to a target page on the server by calling the
Because the transfer between source and target pages happens on the server, the browser has no information about the changed page, and it retains information about the original (source) URL. For example, the Address box in Internet Explorer does not change after a transfer, and instead continues to show the URL of the page it most recently requested (which is usually the source page). The browser's history is not updated to reflect the transfer. This can result in unexpected behavior if the user refreshes the page in the browser or clicks the browser's back button. Therefore, calling the Transfer method is a strategy that is best used in applications where you are presenting pages to the user with the URL hidden.
Selecting a Redirect Option
The following table summarizes the possible ways to redirect between pages.
Strategy | Characteristics | Usage |
---|---|---|
Hyperlinks |
|
|
Cross-page posting |
|
|
Browser redirect |
|
|
Server transfer |
|
|