what is the difference beween response.redirect and server.transfer?

Posted by Raj_Chennai under ASP.NET on 3/9/2012 | Points: 10 | Views : 35307 | Status : [Member] | Replies : 6
what is the difference beween response.redirect and server.transfer?

raj


Responses

Posted by: Shubham on: 3/10/2012 [Member] Starter | Points: 25

Up
0
Down
Dear
One thing to be careful about when using the Server.Transfer is to clear the the HttpResponse object with Response.Clear method on the transfered page to avoid any output from the first page to be shown on the second page.



Response.Redirect should be used when:
.we want to redirect the request to some plain HTML pages on our server or to some other web server
.we don't care about causing additional roundtrips to the server on each request
.we do not need to preserve Query String and Form Variables from the original request
.we want our users to be able to see the new redirected URL where he is redirected in his browser (and be able to bookmark it if its necessary)
Server.Transfer should be used when:
.we want to transfer current page request to another .aspx page on the same server
.we want to preserve server resources and avoid the unnecessary roundtrips to the server
.we want to preserve Query String and Form Variables (optionally)
.we don't need to show the real URL where we redirected the request in the users Web Browser
thank you


Thanks&Regards:-
Shubham Choudhary
Software Engineer
www.facebook.com/shubham.kunar

Raj_Chennai, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hmanjarawala on: 3/12/2012 [Member] Bronze | Points: 25

Up
0
Down
Hi,
The difference between Server.Transfer and Response.Redirect are as follow

1>Response.redirect sends message to the browser saying it to move to some different page while Server.transfer does not send any message to the browser but rather redirects the uyser directly from the server itself.So in Server.transfer their is no round trip while Response.redirect has a round trip hence puts a load on the server.

2>Using Sever.transfer you cannot redirect to a different server itself while using Response.redirect you can redirect to a different server also.

3>With Server.transfer you can preserve your information.It has a parameter called as "preserve form",so the existing query string etc will be avilable in the calling page.This is not possible in Response.redirect

for more information kindly visit these urls
http://www.aspdotnetfaq.com/Faq/what-is-the-difference-between-server-transfer-and-response-redirect-methods.aspx
http://www.dotnetuncle.com/difference/82_servertransfer_responseredirect.aspx



Mark this as answer, if it helps you..............


Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Raj_Chennai, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Susanthampy on: 3/12/2012 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Both "Server" and "Response" are objects of ASP.NET and both are used to transfer a user from one page to another page. Logically both methods are used for the same purpose but still there are some technical differences between both which we need to understand. Differences between both are as follows:

Syntactically both are different. Syntax for sending a user to page "example.aspx" for both method is as below,
Response.Redirect("example.aspx")
Server.Transfer("example.aspx")
Response.Redirect involves an extra round-trip to the server whereas Server.Transfer() conserves server resources by avoiding that extra round-trip. Server.Transfer() just changes the focus of the page by directly communicating with server.
Round-trip - Response.Redirect() first sends request for new page to the browser, then browser sends that request to the web-server, and after that your page changes. But Server.Transfer() directly communicate with the server to change the page hence it saves an extra round-trip (to the browser) in the whole process.

If you are using Server.Transfer() then you can directly access the values, controls and properties of the previous page which you can't do with Response.Redirect().
Suppose you have redirected user from Page1.aspx to Page2.aspx using Response.Redirect() then when user is transffered to Page2.aspx then Pag1.aspx has been flushed from the server's memory and hence no information of previous page are accessible, unless the developer explicitly saved the information using some other methods like session, cookie, application, cache etc. But in case of Server.Transfer variables can stay in scope and Page2.aspx can access values, controls, etc. of previous page Page1.aspx as it's still in memory, because Server.Transfer just changes the focus from Page1.aspx to Page2.aspx, so in this case browser doesn't know that any change is happen and which is why with Server.Transfer() we can access values of previous page.

Response.Redirect() changes the URL in the browser's address bar, so they can be bookmarked. Whereas Server.Transfer() retains the original URL in the browser's address bar, it just replaces the contents of the previous page with the new one and hence bookmarking is not works properly.
Server.Transfer() directly communicate with the server for new page request, and hence browser doesn't aware that there is any change happened. Whereas Response.Redirect() first send request (for new page) to browser, then further processing will be performed, so here browser knows that there is some change in the browser window that's why it changes the URL in the address bar.
We can't call it as a problem "No change of address in the browser address bar" it depends on how you see it. This is a good if you see it from security point of view, but it is a problem if you refresh your page or you wanted to bookmark that page. Browsers perform Refresh and Bookmark operations on the URL which is present in the address bar, but as we know Server.Transfer() doesn't changes the URL, so sometimes its a problem with this method.

Response.Redirect() can be used for both .aspx and .html pages, whereas Server.Transfer() can be used only for .aspx pages and is specific to ASP and ASP.NET.
With Response.Redirect() you can redirect the user to the both type of pages .html or .aspx as below,

Response.Redirect("example.html")
Response.Redirect("example.aspx")
But Server.Transfer() only work with .asp or .aspx page as show below

Server.Transfer("example.asp")
Server.Transfer("example.aspx")
Response.Redirect() can be used to redirect to an external websites, whereas we cannot use Server.Transfer() to redirect to an external website. We can use Server.Transfer() to redirect only inside the same website.
Let's say on click of a link I want to redirect user to http://www.yahoo.com/ . With Response.Redirect() you can redirect user to the external site, but in case of Server.Transfer() wouldn't let you perform this operation, because Yahoo.com resides on different server than yours. Server.Transfer() only work with the .asp or .aspx pages present inside your web application.

Now the question arises which to use and when to use? Mostly Server.Transfer() method is recommended to use because it is faster because it saves a round-trip in the process of redirecting from one page to another, but some people says that Server.Transfer() is not recommended to use because operations typically flows through several different pages due to which you will lose the correct URL of the page in the browser address bar, but again all depends on your situation and requirements.

Server.Transfer() also allows more flexibility as you can use HTTPContext.Items to pass variables between pages, use Server.Transfer() when you need to pass context items. Otherwise use Response.Redirect() as it shows correct URL in the address bar.

Refer this link,

http://www.dotnetexpertguide.com/2010/03/aspnet-difference-between.html

Regards,
Susan

Raj_Chennai, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Patel28rajendra on: 3/13/2012 [Member] Starter | Points: 25

Up
0
Down
Hi

1. Response.Redirect will round trip to the server where as Server.tranfer does not make round trip
2. Response.Redirect will change browser current url and update history where as server.tranfer does not change browser current url
3. Response.redirect is slow as it makes round trip to the server where as server.tranfer is fast compared to it

Thanks

Rajendra Patel

R D Patel

Raj_Chennai, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: sudhakaradapa45-19948 on: 12/24/2012 [Member] Starter | Points: 25

Up
0
Down
Hi Susanthampy ,

in server.transfer("example.html"); also it is working, i tryed with this, so please correct ur answer

sudhakar Adapa

Raj_Chennai, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Subramanyame on: 12/27/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,


Response.Redirect :
Is used to navigate from one web site to another website by providing URl
And also navigation within the page.
Once redirection takes place then browser URL is changed.

1.Response.Redirect("Example.aspx");
2.Response.Redirect("http://www.Employees/list");

Server.transfer :

Used in within the website only.
doen't support URl and also not refresh the browser URL.
In Viewstate, by using server.transfer we can get the values of previous page into present Page.


Raj_Chennai, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response