Q: How do I send a new page to the user?
A: The Redirect method of the Response object is one of the most common ways to
send users a new Web page:
Response.Redirect("Page2.aspx")
You might also know of the similar method, Server.Transfer:
Server.Transfer("Page2.aspx")
But do you know the difference between these two methods? The server-side Response.Redirect
statement sends a command back to the browser to request the next page from the
server. This extra round-trip is often inefficient and unnecessary, but this battle-tested
standard works reliably and consistently. By the time Page2 is requested, Page1
has been flushed from the server’s memory and no information can be retrieved about
it unless the developer explicitly saved the information using one of the techniques
outlined later in this article.
The more efficient Server.Transfer method simply renders the next page to the browser
without the extra round trip. Variables can stay in scope and Page2 can read properties
directly from Page1 because it’s still in memory. This technique would be ideal
if it wasn’t for the fact that the browser is never notified that the page has changed.
Therefore, the address bar in the browser will still read “Page1.aspx” even though
the Server.Transfer statement actually caused Page2.aspx to be rendered instead.
While this bit of sneakiness can occasionally be a good thing from a security perspective,
it often causes problems related to the browser being out of sync with the server.
For example, if the user reloads the page, the browser will request Page1.aspx instead
of the true page (Page2.aspx) that they were viewing.
In most cases, Response.Redirect and Server.Transfer can be used interchangeably.
But in some cases, efficiency or usability may prompt you to use one instead of
the other.