Request.Redirect vs. Server.Transfer

Posted on: Wed Aug 06 14:49:37 -0700 2008. Updated on: Wed Aug 06 14:49:37 -0700 2008.
Category: DotNet

I often forget which one of these to use in what scenario:

Server.Transfer("~/admin/someurl.aspx");

Request.Redirect("/control/anotherurl.aspx");

In essence, they do the same thing, simply take you to the page you specify as a parameter. But they do this in different ways.

Request.Redirect actually sends a 304 and loads the url as if you typed it in and hit enter. You should use this when you need to pass querystrings in the redirection url. The url at the top will actually change, so any previous querystrings will not stick. Also, you cannot use the PreviousPage c sharp command to have access to variables in the page doing the redirection.

Server.Transfer on the other hand does not hit the client. The url in you browser window will not change although you will be in a different page. This means previous querystring will stick. This also means you shouldn't use it to pass querystrings in the redirection url. The cool thing about it is that you can use PreviousPage on the page you transferred to so as to get access to variables in the transferred from page.
In load balanced scenarios, Server.Transfer will actually keep the object and the client connected to the same sever. If you use Request.Redirect the client could end up requesting from another server. Depending on what it is you are trying to do, keep this in mind.

Comments:

Add a Comment:

simple_captcha.jpg
(human authentication)


Ads

Categories

About

Random foliage

This website is meant to be a reference for ASP Dot Net developers. The entries are a compilation of things I've figured out how to do and that I deem useful to keep of track for future reference. Assumptions: web development with: C Sharp (vb sucks), visual studio 05/08, .net 3.5, meant for programmers. Written by: James Reategui.