4

I found many topics on Server.Transfer VS Response.Redirect but none of them explained about the difference between the Server.Transfer and Response.Rewrite.

As far as I know, they use the same type of method for navigating the user: So what is the difference between them and when should they be used?

dsynkd
  • 1,999
  • 4
  • 26
  • 40
  • possible duplicate of [Which one is better Server.Transfer and Response.Redirect](http://stackoverflow.com/questions/5151439/which-one-is-better-server-transfer-and-response-redirect) – slugster Jan 15 '12 at 06:27
  • The pivotal difference is that Response.Redirect round trips back to the web browser to tell it to redirect to a new URL, whereas Server.Transfer just starts processing a new page on the server without the round trip. – Eric J. Jan 15 '12 at 06:31
  • 1
    It just feels wrong to close for an answer with -1 votes and no accepted question. Perhaps we could get a good accepted answer in here, as this is a better question, and close the other one for this? (Or perhaps a *decent* duplicate can be linked...) –  Jan 15 '12 at 07:03
  • 2
    I asked for the difference between response.rewrite and server.transfer but friends are all answering for response.redirect. – dsynkd Jan 17 '12 at 14:12
  • I guess my question was not clear. I changed it a bit... hope it helps – dsynkd Jan 17 '12 at 14:13
  • V0R73X: I think @pst misread things when he edited your question and removed the key references to rewrite. Your original was clearer but the latest is much better. :) – Chris Jan 17 '12 at 14:21
  • @Chris Indeed I did: I somehow transposed `Redirect` for `Rewrite`. Silly alliterative words. –  Jan 17 '12 at 21:10

2 Answers2

0

Response.Redirect will send redirect headers from server to client, and will cause another request to the new url (total: 2 requests).

However, Server.Transfer will cause only 1 client-server request, and the url in the browser address bar won't change because the browser didn't get any sign for redirect, it all happened in the server side- unlike Response.Redirect.

Hope that helps.

Gal V
  • 32
  • 3
0

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").

So, in brief: 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.