How to use Server.Transfer("default.aspx")
for better performance for navigating within the website. When I use this, it is not changing the url in the address bar. How can I achieve new url by server.transfer. Or (If Not) how can I gain performance with Response.Redirect("default.aspx")
.

- 7,537
- 24
- 66
- 107
-
What do you mean by same performance? A response.Redirect will in theory run exactly the same code as a server.transfer, the only additional latency would be the http redirect which would be minimal. – ChrisBint Nov 20 '12 at 13:27
-
It's absolutely **not something about performance but about intents**. Use what's appropriate!!! – Adriano Repetti Nov 20 '12 at 13:28
-
@ChrisBint : Not taking so much time to navigate or extra round trip – Rajaram Shelar Nov 20 '12 at 13:29
-
@eraj It's all about perception in my opinion. You don't see the server.transfer, but you do see the redirect. As said, the code itself will remain identical so that is not a factor. You are just left with the perceived experience of it taking longer. I honestly doubt that it is and anyway, there is nothing you can do about it, you either transfer or redirect. – ChrisBint Nov 20 '12 at 13:33
2 Answers
You need to understand the difference between Response.Redirect("page.aspx")
and Server.Transfer("page.aspx")
Server.Transfer:
It does not change the URL, so it is not for debugging purposes because you are not certain which page is running down in the browser since URL might not changed in more than one
Server.Transfer
statements.It posts data from all controls on the Form on to the next page, from where you can access them using
Request.Form["myTextBox"]
It only works within the same domain, it will not redirect outside the current domain name.
It does not cost a round trip on server back from browser, so it is faster as compared to
Response.Redirect
.
Use your best judgement when to use Response.Redirect
and when to use Server.Transfer
. I would only recommend using 'Server.Transfer' if you want to send Form Controls' data from one page to other, otherwise it will give you a debugging nightmare.

- 84,970
- 20
- 145
- 172

- 366
- 2
- 5
-
Yes. Totally agree with you but i want something more. Is it possible to have new url in the next page with server.transfer?. Or how effectively handle response.redirect? – Rajaram Shelar Nov 20 '12 at 14:11
-
5@eraj - During `Server.Transfer`, nothing is sent back to the browser. Whatever the transferred page produces as output gets sent back to the browser. There's no mechanism, in a response, to say "please now adjust the address bar to *this value*" - imagine the fun that phishers could have with such a mechanism. If you want the browser to know about which page it's on, `Response.Redirect`. – Damien_The_Unbeliever Nov 20 '12 at 14:48
-
1Eraj, that is not possible. You cannot modify URL with `Server.Transfer`. Then you call Server.Transfer, it redirects to next page right from the server, without sending control back to browser. But when you call Response.Redirect, it sends response back to browser. Then browser redirects the page to new page, alongwith changing its URL. I hope that answers your question, if yes, mark it as "answered". – raheel khokhar Nov 20 '12 at 15:30
-
@raheel khokhar: Thanks for the responses.But these are all about the mechanism that is used in server.transfer() and response.redirect(). I want answer like setting or code that can illustrate the efficient way of handling redirection. – Rajaram Shelar Nov 20 '12 at 17:40
-
I personally do not think that you can enhance performance of a Response.Redirect, since it will follow the full mechanism it is supposed to:
Event from client >> Posts back to Server >> Runs redirection code >> Sends response back to browser >> browser redirects to the new page But if you use javascript based redirection, you will save lots of above-mentioned steps: Event from client >> Runs redirection code >> Browsers redirects to new page. You can try using: – raheel khokhar Nov 20 '12 at 18:46
I'm not sure I like the approach much, but if you insist on using Server.Transfer
you could use the HTML5 History API
to change the URL in the browser address bar once your response reaches the browser and is being processed there. Beware that only newer browsers support this functionality but as time passes this should stop to be a problem.
You would need a piece of JavaScript in your page to manipulate the current state of the history. This would look something like:
<script type="text/javascript">
window.history.pushState({ path: <pageurl> }, '', <pageurl>);
</script>
The <pageurl>
placeholder has to be set on the server to the real URL of the page you're actually processing in your Server.Transfer
call.
There are tons of examples of how to use the html5 history api on the net by now, e.g. http://html5demos.com/history.

- 9,239
- 9
- 69
- 100