views:

1665

answers:

7

Which is better, Response.Redirect or Server.Transfer in ASP.NET ?

+13  A: 

Response.Redirect redirects page to another page after first page arrives to client. So client knows the redirection.

Server.Transfer quits current execution of the page. Client does not know the redirection. It allows you to transfer the query string and form variables.

So it depends to your needs to choose which is better.

Canavar
+7  A: 

Canavar beat me to it :)

I would add that Server.Transfer reduces page requests, so I suppose it's "better" in that respect. However, Response.Redirect can send the user to an external site while Server.Transfer can't.

whatknott
I've forgot this : "Response.Redirect can send the user to an external site while Server.Transfer can't." :):)
Canavar
+1  A: 

Server.Transfer doesn't change the URL in the client browser, so effectively the browser does not know you changed to another server-side handler. Response.Redirect tells the browser to move to a different page, so the url in the titlebar changes.

Server.Transfer is slightly faster since it avoids one roundtrip to the server, but the non-change of url may be either good or bad for you, depending on what you're trying to do.

krosenvold
+5  A: 

In addition to ScarletGarden's comment, you also need to consider the impact of search engines and your redirect. Has this page moved permanently? Temporarily? It makes a difference.

see: Response.Redirect vs. "301 Moved Permanently":

We've all used Response.Redirect at one time or another. It's the quick and easy way to get visitors pointed in the right direction if they somehow end up in the wrong place. But did you know that Response.Redirect sends an HTTP response status code of "302 Found" when you might really want to send "301 Moved Permanently"?

The distinction seems small, but in certain cases it can actually make a big difference. For example, if you use a "301 Moved Permanently" response code, most search engines will remove the outdated link from their index and replace it with the new one. If you use "302 Found", they'll continue returning to the old page...

Diodeus
+4  A: 

To be Short: Response.Redirect simply tells the browser to visit another page. Server.Transfer helps reduce server requests, keeps the URL the same and, with a little bug-bashing, allows you to transfer the query string and form variables.

Something I found and agree with that Server.Transfer is similar in that it sends the user to another page with a statement such as Server.Transfer("WebForm2.aspx"). However, the statement has a number of distinct advantages and disadvantages.

Firstly, transferring to another page using Server.Transfer conserves server resources. Instead of telling the browser to redirect, it simply changes the "focus" on the Web server and transfers the request. This means you don't get quite as many HTTP requests coming through, which therefore eases the pressure on your Web server and makes your applications run faster.

But watch out: because the "transfer" process can work on only those sites running on the server, you can't use Server.Transfer to send the user to an external site. Only Response.Redirect can do that.

Secondly, Server.Transfer maintains the original URL in the browser. This can really help streamline data entry techniques, although it may make for confusion when debugging.

That's not all: The Server.Transfer method also has a second parameter—"preserveForm". If you set this to True, using a statement such as Server.Transfer("WebForm2.aspx", True), the existing query string and any form variables will still be available to the page you are transferring to.

For example, if your WebForm1.aspx has a TextBox control called TextBox1 and you transferred to WebForm2.aspx with the preserveForm parameter set to True, you'd be able to retrieve the value of the original page TextBox control by referencing Request.Form("TextBox1").

TStamper
Ironically, "To be short", this is the longest answer!
spoulson
lol....the first paragraph was the short answer part
TStamper
+1 for comment but this seems copied verbatim from http://www.developer.com/net/asp/article.php/3299641. If it is from another source you should at lease cite it.
John Nolan
... but they have copied it they should cite you.
John Nolan
I said: Something I found and agree with;
TStamper
Should link to source and use quote formatting/highlighting for the copied parts.
Chris W. Rea
+1  A: 

Detailed article on the subject:

What is the difference between Server.Transfer and Response.Redirect methods?

Roboblob
+2  A: 

If you're running on IIS 7 Integrated mode, you might consider using Server.TransferRequest (see http://bit.ly/awu5Vw) instead of Server.Transfer.

Haacked