I still remember the old days when I first learned ASP.NET 1.0, I used to search, like other newcomers, for the differences between Response.Redirect and the Server.Transfer. I found answers such as Server.Transfer had been kept in ASP.NET for backward compatibility with ASP so the recommendation was to use Response.Redirect or that I should be using the Server.Transfer for better performance!
The truth is every method has a completely different and important use.
Response.Redirect : When you call this method, ASP.NET will issue HTTP headers that will instruct the browser for the new page tonavigate to and will stop processing any further ASP.NET instruction. The following code:
Response.Redirect("default2.aspx");
throw new Exception("This exception will not be executed!");
will redirect to default2.aspx and will not execute the next line. It will issue an HTTP header instruction to the browser as follows:
Location: /default2.aspx
Then the browser will request the new page "default2.aspx".
Server.Transfer : This method will transfer execution ON THE SERVER from the current executing page to the new page "default2.aspx" without notifying the client (the browser) of this change. Also, will stop executing any further instruction on the current page.
Server.Transfer("default2.aspx");
throw new Exception("This exception will not be executed!");
Comparison
| | Redirect | Transfer | Commets |
| Better performance | | X |
Redirect involves the extra overhead of going back and forth between the server and the client while Transfer is done completely on the server |
| Change the URL in the browser’s address bar | X | |
Transfer will not notify the browser with the change of pages |
| AJAX and JavaScript Friendly | X | |
Since Transfer will not notify the browser with the change of page, some Ajax operations might fail as well as some JavaScript operations |
| Can carry simple information securely from one page to another | | X |
Both methods can transfer simple data from one page to another via query strings, however, with Redirect the client will be able to read the information from the query string. Encrypting the query string is another topic |
| Can carry complex objects from one page to another | | X |
Doing Transfer ("default2.aspx", true), you can still access the objects of default.aspx from default2.aspx when default2.aspx first loads |
| Errors can easily be traced | X | |
Using Transfer, errors might appear in your error log as if they are coming from default.aspx when they might be coming from default2.aspx |
So what does all this mean? Simply, use the Redirect method by default unless you need to transfer complex data from one page to another, pass the data securely or hide the new page from the client.
Please let me know if you have additional differences or similarities that haven't been mentioned.