{"id":90,"date":"2006-05-12T15:53:04","date_gmt":"2006-05-12T14:53:04","guid":{"rendered":"http:\/\/www.galhano.com\/blog\/?p=90"},"modified":"2008-02-07T18:38:36","modified_gmt":"2008-02-07T17:38:36","slug":"servertransfer-vs-responseredirect","status":"publish","type":"post","link":"http:\/\/galhano.com\/blog\/?p=90","title":{"rendered":"Server.Transfer Vs. Response.Redirect"},"content":{"rendered":"<p><font size=\"-1\" face=\"Verdana, Arial, Helvetica\">If you read a lot of industry magazines and ASP.NET code samples, you may find that, although the majority use <u>Response.Redirect<\/u> to send the user to another page, some seem to prefer the rather mysterious-sounding <u>Server.Transfer<\/u>. So, what&#8217;s the difference?<\/font><\/p>\n<p><font size=\"-1\" face=\"Verdana, Arial, Helvetica\">Well, <u>Response.Redirect<\/u> simply sends a message down to the browser, telling it to move to another page. So, you may run code like: <\/font><\/p>\n<pre><font size=\"-1\" face=\"Verdana, Arial, Helvetica\">Response.Redirect(\"WebForm2.aspx\")<\/font><\/pre>\n<p><font size=\"-1\" face=\"Verdana, Arial, Helvetica\">or<\/font><\/p>\n<pre><font size=\"-1\" face=\"Verdana, Arial, Helvetica\">Response.Redirect(\"http:\/\/www.karlmoore.com\/\")<\/font><\/pre>\n<p><font size=\"-1\" face=\"Verdana, Arial, Helvetica\">to send the user to another page.<\/font><\/p>\n<p><font size=\"-1\" face=\"Verdana, Arial, Helvetica\"><u>Server.Transfer<\/u> is similar in that it sends the user to another page with a statement such as <u>Server.Transfer(&#8220;WebForm2.aspx&#8221;)<\/u>. However, the statement has a number of distinct advantages and disadvantages.<\/font><\/p>\n<p><font size=\"-1\" face=\"Verdana, Arial, Helvetica\">Firstly, transferring to another page using <u>Server.Transfer<\/u> conserves server resources. Instead of telling the browser to redirect, it simply changes the &#8220;focus&#8221; on the Web server and transfers the request. This means you don&#8217;t get quite as many HTTP requests coming through, which therefore eases the pressure on your Web server and makes your applications run faster.<\/font><\/p>\n<p><font size=\"-1\" face=\"Verdana, Arial, Helvetica\">But watch out: because the &#8220;transfer&#8221; process can work on only those sites running on the server, you can&#8217;t use <u>Server.Transfer<\/u> to send the user to an external site. <u>Only Response.Redirect<\/u> can do that.<\/font><\/p>\n<p><font size=\"-1\" face=\"Verdana, Arial, Helvetica\">Secondly, <u>Server.Transfer<\/u> maintains the original URL in the browser. This can really help streamline data entry techniques, although it may make for confusion when debugging.<\/font><\/p>\n<p><font size=\"-1\" face=\"Verdana, Arial, Helvetica\">That&#8217;s not all: The <u>Server.Transfer<\/u> method also has a second parameter\u00e2\u20ac\u201d&#8221;preserveForm&#8221;. If you set this to <u>True<\/u>, using a statement such as <u>Server.Transfer(&#8220;WebForm2.aspx&#8221;, True)<\/u>, the existing query string and any form variables will still be available to the page you are transferring to.<\/font><\/p>\n<p><font size=\"-1\" face=\"Verdana, Arial, Helvetica\">For example, if your WebForm1.aspx has a TextBox control called TextBox1 and you transferred to WebForm2.aspx with the <u>preserveForm<\/u> parameter set to <u>True<\/u>, you&#8217;d be able to retrieve the value of the original page TextBox control by referencing <u>Request.Form(&#8220;TextBox1&#8221;)<\/u>.<\/font><\/p>\n<p><font size=\"-1\" face=\"Verdana, Arial, Helvetica\">This technique is great for wizard-style input forms split over multiple pages. But there&#8217;s another thing you&#8217;ll want to watch out for when using the <u>preserveForm<\/u> 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&#8217;ll find this documented at <a target=\"new\" href=\"http:\/\/support.microsoft.com\/default.aspx?id=kb;en-us;Q316920\">http:\/\/support.microsoft.com\/default.aspx?id=kb;en-us;Q316920<\/a>.<\/font><\/p>\n<p><font size=\"-1\" face=\"Verdana, Arial, Helvetica\">The unofficial solution is to set the <u>enableViewStateMac<\/u> property to <u>True<\/u> on the page you&#8217;ll be transferring to, then set it back to <u>False<\/u>. This records that you want a definitive <u>False<\/u> value for this property and resolves the bug.<\/font><\/p>\n<p><font size=\"-1\" face=\"Verdana, Arial, Helvetica\">So, in brief: <u>Response.Redirect<\/u> simply tells the browser to visit another page. <u>Server.Transfer<\/u> 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.<\/font><\/p>\n<blockquote><p>\n<font size=\"-1\" face=\"Verdana, Arial, Helvetica\"><strong>Top Tip:<\/strong> Don&#8217;t confuse <u>Server.Transfer<\/u> with <u>Server.Execute<\/u>, which executes the page and returns the results. It was useful in the past, but, with ASP.NET, it&#8217;s been replaced with fresher methods of development. Ignore it.<\/font>\n<\/p><\/blockquote>\n<p><!--7c89591cdc1f2c787b6536aefaf933d1--><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;s the difference? Well, Response.Redirect simply sends a message down to the browser, telling it to move to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"close","ping_status":"close","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-90","post","type-post","status-publish","format-standard","hentry","category-aspnet","author-admin"],"_links":{"self":[{"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/90","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=90"}],"version-history":[{"count":0,"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/90\/revisions"}],"wp:attachment":[{"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=90"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=90"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=90"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}