Server.Transfer vs. Response.Redirectby Karl Moore
If you read a lot of industry magazines and ASP.NET code samples, you may find that, although the majority use Response.Redirect to send the user to another page, some seem to prefer the rather mysterious-sounding Server.Transfer. So, What's the difference?
Well, Response.Redirect simply sends a message down to the browser, telling it to move to another page So, you may run code like:. Response.Redirect ( "WebForm2.aspx") orResponse.Redirect ( "http: // www Karlmoore.com / ") TO Send The User To Another Page. Server.Transfer IS Similar IN That Reedment 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 Resp onse.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"). This technique is great for wizard-style input forms split over multiple pages. But there's another thing you'll want to watch out for when using the preserveForm parameter. ASP.NET has a bug whereby, in certain situations, an error will occur when attempting to transfer the form and query string values. You'll find this documented at http://support.microsoft.com /default.aspx?id=kb;en-us;Q316920. The unofficial solution is to set the enableViewStateMac property to True on the page you'll be transferring to, then set it back to False. This records that you want a definitive False value for this property and resolves the bug 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.top Tip: Don't Con FUSE
Server.Transfer with
Server.execute, Which Executes The page and return the results. It's. Useful in The Past, But, with asp.net, it's.
About the Author Karl Moore (MCSD, MVP) is an experience author living in Yorkshire, England. He is author of numerous technology books, including the new Ultimate VB .NET and ASP.NET Code Book, plus regularly features at industry conferences and on ..................