| Introduction This article tell you the difference between Server.Transfer and Response.Redirect. |
Server.Transfer v/s Response.Redirect Both “Server” and “Response” are objects of ASP.NET. Server.Transfer and Response.Redirect both are used to transfer a user from one page to another page. Both are used for the same purpose but still there are some differences are there between both that are as follows: |
Response.Redirect(“newpage.aspx”) and Server.Transfer(“newpage.aspx”) |
Roundtrip means in case of Response.Redirect it first sends the request for the new page to the browser then browser sends the request for the new page to the webserver then after your page changes But in case of Server.Transfer it directly communicate with the server to change the page hence it saves a roundtrip in the whole process.
Suppose you are currently on the Page1.aspx and now you are transferring the user to the Page2.aspx using Response.Redirect then When the Page2 page is requested, Page1 has been flushed from the server’s memory and no information can be retrieved about it unless the developer explicitly saved the information using some technique like session, cookie, application, cache etc. But in case of Server.Transfer variables can stay in scope and Page2 can read properties directly from Page1 because it’s still in memory, as you know the Server.Transfer just changes the focus from page1 to page2 So in this case browser doesn’t know that any change is happen there that’s why with this method you can access the information about the previous page.
Actually in case of Server.Transfer it directly contact with the webserver for the new page request, it doesn’t involve the browser, so that browser doesn’t know that there is any change happen. But in case of Response.Redirect as you know it first send the request (for new page) to the browser then further processing will be performed, so here browser knows that yes there is some change in the browser window that’s why it changes the URL in the address bar.
With Response.Redirect you can redirect the user to the both type of pages .html or .aspx like below,
Suppose on some action on the webpage I want to redirect my user to the http://www.yahoo.com so with Response.Redirect you can redirect your user to the external site, but in case of Server.Transfer you can only work with the .asp or .aspx pages that are present in your site. Now the question is which to use and when to use? Mostly the Server.Transfer method is preferable to use because Server.Transfer is faster since there is one less roundtrip, but in some people say that Server.Transfer is not recommended since the operations typically flow through several different pages due to which you lose the correct URL of the page, but again all depends on your requirement. I mentioned the most common differences here if anyone knows any other difference other than these then please give your comments. Thank you, |
|
******* |
ASP.NET – Server.Transfer v/s Response.Redirect
October 14, 2007 by Prashant Pandey

very interesting. i was always wondering what the difference was between these. thanks
thats was quite good… I knew this before, very well explained anyways!!
it is really a very good explanation………………….
because i did not find these types of differences till now
thank u ………..
Very good article very good explication.
Compare to my article this is very very nice thank for your feedback
Very good article very good explanation.
it has really helped me to understand the subject matter. thanks.
very informative… now everyone knows wat the actuall diff is..
This is very simple. and easy to understand the difference between server.transfer and request.redirect with example
thank you once again for this article.
Explained very well & clear unlike other websites. This arcticle is what i’m looking for. Thanks
hey very well explained actually i am having some doubts regarding these 2 things now its clarified.
Very good explaination.It really helped to understand the difference.I was little confused before regards this concept but now i was very clear.Thanks a lot
Beautifully Explained i think no need of further explanation of it
Fantastic explanation, it helped me a lot. Thanks a lot.
Hi,
Its very well explained actually i am having some doubts regarding these 2 things now its clarified.
if u don’t mind , pls give me an example how we can use variavles,valus etc of firstpasge.aspx in the secondpage.aspx when we use server.transfer() for redirect firstpasge.aspx to
secondpage.aspx.
Thanx & Regard
Bachan
Its an excellent article. i have never seen like this. very well explained. thanks a lot.
very well explained.
Very well explained … Thanks
It is very well explained. thanks for giving a clear idea about the differences, with examples. An excellent article.
thanks & Regards,
krishnaveni
Its Really Super …. nice
See Suppose , we have two servers A & B , A is exposed to client, so when a client request the page the reponse will be sent from A . Whereas client cant access B server, as this is not exposed to client. Only Server A can access server B , as these two servers are in same network and domain .
So now the question is, I am having all my PDF documents in server B , so through server A i can access the PDF files using BInary Read & Write.
Example : B server addres is “http://bserver/pdfdocs?id=222″
so in b server it has been configured that only server “A” can access B server. USing ASP or ASP.net i can download the Binary data from B server to A server. Now the question is how can I give the link to client , so that the binary data could be downloaded from B server. Client cant access B server directly.
I used Reponse.redirect (”http://bserver/pdfdocs?id=222″) in my intranet environment . Because the link “http://bserver/pdfdocs?id=222″ will return the binary data as bytes. so giving response.redirect will download the data .
So, will this work in INternet with the same Response.Redirect, according to you Response.redirect will have a roundtrip, means first it will sent the url to the client browser then again from client browser to requested URl, so will it work ??????
Hope I confused a lot all of you.
Hi Muhammad Yahya,
“Hope I confused a lot all of you.”
yes you have confused me a lot,
“when a client request the page the response will be sent from A . Whereas client cant access B server”
First tell me that whenever the client will generate the request then that request will be sent via A server to B Server, If this will be the case then client can access the B Server via A Server.
Now if you will give the link “http://bserver/pdfdocs?id=222″ to the client and he’s accessing this link via A Server (that client doesn’t know that he’s accessing this link via a mediator that is A Server) then client can access and download these files.
And if the above is teh case that client will access the B Server via A Server not directly then
Response.redirect (”http://bserver/pdfdocs?id=222″) this will work in an INTERNET environment.
Please tell me if i am wrong??, and if you already find the solution then please do share it with us.
Thanks,
Reauthorization
ASP.NET does not verify that the current user is authorized to view the resource delivered by the Transfer method. Although the ASP.NET authorization and authentication logic runs before the original resource handler is called, ASP.NET directly calls the handler indicated by the Transfer method and does not rerun authentication and authorization logic for the new resource. If your application’s security policy requires clients to have proper authorization to access the resource, the application should force reauthorization or provide a custom access-control mechanism.
You can force reauthorization by using the Redirect method instead of the Transfer method. The Redirect method performs a client-side redirect in which the browser requests the new resource. Because this redirect is a new request entering the system, it is subjected to all the authentication and authorization logic of both Microsoft Internet Information Services (IIS) and ASP.NET security policy.
You can verify that the user has permission to view the resource by incorporating a custom authorization method that uses the IsInRole method before the application calls the Transfer method.
It is Good
Extrreeeeamly good explained….
It cleared all my doubts.
Thanks.
Very informative… Keep posting like this..
Perfect explanation,
It helped me a lot.
Thanks in advance…
appreciations, always boost confidence, thanks to all of you for your appreciation,
keep reading the blog, to get ahead…
Thanks,
Prashant
(techAhead)
Hi,
Thanx a lot
It’s very easy to understand and also most of the things are clarified. It was quite good for differentiation.
Thanks a lot.
Thanks a lot. U explained it very nicely.
Nice Job ! Very Well explained !
Hi, (ASP.net 2008 Problem)
Can anyone tell me that what to write in COMMAND BUTTON CODE to Move PreviousPage of the Internet Explorer.
Thanks.
Hi Satish,
I haven’t used VS2008 yet, but may be you can solve your problem with using following javascipt:
javascript:window.history.back(1);
Thanks,
Prashant
Thanks, It’s a nice article
well explained and clear my all doubt except
“use Server.Transfer when you need to pass context items. Otherwise use Response.Redirect so the user will always see the correct URL in the address bar. ”
whats its meaning.
Simply damn good. Each dimension is touched and cleared very well. But it still needs some add ons for technical backend details.
But yes its pretty fine to understand the core difference between the both.
it is very good bt what do u mean by bookmark in this contest and what do u mean by within the same server.
Hi Dilip,
Bookmark:
Here suppose yo have two pages, page1.aspx and page2.aspx and you have a button on page1.aspx onclick of that button you have transfered user to page2.aspx using Server.Transfer method, in that case the URL in the address bar will not change. So if user want to bookmark page2.aspx then he can’t caz url displayed in the address bar is page1.aspx. Means if user will not notice the url then he/she will bookmark a wrong page.
Within The Same Server:
this means you can’t redirect a to http://www.yahoo.com using server.transfer caz it works only for the url’s of the same server where your application is running. Whereas response.redirect will redirect you to yahoo’s website.
Thanks,
Prashant
Thanks for these informations
Comprhensive article..Good Job
Very good and comprehensive explanations.
Many Thanks
Thanks for better explanation
Wow! great explanation! keep it up!
Important
with Server.Transfer you canaccess the PreviousPage (Property)
AND
pass variables with HTTPContext.Items
Peter
hello,
i have one problem in my application related to these properties.I am describing…
I have two web applications.One is installed at client side.it is working in intranet currently(after 2 months it is going to be on web).Another Is at my side which is running on the web.My problem is ,i want to update master data from web application to client application.for that i have created one screen in client app. from where it connects to web application’s master data screen using response.redirect.i have checked that it also passes parameters(string values) correctly from client side to server side and vice versa.but i am unable to pass large amount of data(in form of datagrid,datatable)..i have tried ’server.transfer’ but i found that it works only within it’s own server app.is there any way so that i can pass the data (in any format i.e. datatable,dataset,datagrid control,session) to the client page..?i have also tried to pass with session:
Dim dt As New DataTable
dt = CType(Session(”Data”), DataTable),but this thing also works within the own server app,Is there any way to retrive server side session in javascript ..someting like that
or can server.transfer can be used to navigate between to different applications?plz help….
Hi Saurabh,
What i understand from your description is as follows!
You have an application which is live and running on web. and also you have another application which running at your local PC. Now you want data of your live application on local application.
If its true, then i think you should use SQL DTS (in SQL 2000) Or SSIS (in SQL 2005) to get the live site data on local. Caz getting huge data through web application on local is a tedious and long process. You can use SQL services which i have mentioned above. They’ll do your task quickly.
I think you are not able to get the session data caz both the applications are running inside the different domains (internet and local).
According to me you should use SQL Services to get your huge data on local. like SQL Backup, or SQL DTS, SSIS.
Feel free to ask any other query.
Thanks!
thank u for the reply..
Can U plz tell me if i want to do this in application then how should i do it? because i dont want the client side to interfere the database (sql2000). I also tried another way..
i made a connstring which is having static ip addr of web server,but it also doesnt work..(Provider=sqloledb;Data Source=xxx.xxx.xxx.xxx,1433;Network Library=DBMSSOCN;Initial Catalog=xxxx;User ID=myUsername;Password=myPassword;)
Even if it is a long ,tedious task it is going to be processed once in a week(master data updation).actually i am going to clear the previous master data and insert new one using application.it is littile risky to give this in hand of users(DTS).
i have already searched the net and havent found issue like this..can u plz show me the way..i need help badly…
Hi Saurabh,
I think if you want do it using application, Then, what’s the best and possible way i am feeling is XML file, caz its not possible to pass this much amount of data with Query String parameters, and also as both app’s are on different domains so i don’t think we can make use of session also.
So what I suggest you is use XML as in the following way!
From your web application which is running live, get all your data in the dataset then generate XML file from dataset.
ds.WriteXml(Server.MapPath(”../app_data/admin-xml-data/search-settings.xml”));
After that import that XML file in dataset,
ds.ReadXml(Server.MapPath(”../app_data/admin-xml-data/search-settings.xml”));
on your local we app and insert your data on local one by one.
I suppose this is the best way!
Try this if it works for you else tell me, I’ll find some other way!
Thanks,
Prashant
i understood what u said i will make it that way, but how will i pass file back to the client application ..?
scene is that :
in client application i have given a screen which is having a button through which i open a window(using
window.open(http://my web app..) ),in that screen i am filling the data on load event.. as u r saying even if i fill that data using xml how will i send it to the client screen which is just behind the web app screen..it just like u r thirsty and tied near a river..
is it possible using response.redirect? or using some another code.
hello i started coding as u said..
ds.wrtitexml was done successfully but while reading it gives error as ‘virtual path is invalid..i think this is because ‘http://’ is not allowed in server.mappath..So can u plz tell me how to access the xml file (which is residing on server)from client side ?
here is some code for client side :
Dim ds As New DataSet
Dim request As WebRequest = WebRequest.Create(”http://xxx.xxx.xxx.xxx/appname/forms/files/data.xml”)
request.Method = “GET”
request.ContentType = “text/xml”
Dim reader As New StreamReader(Page.Request.InputStream)
reader.ReadToEnd()
ds.ReadXml(reader)
DatagridList.DataSource = ds
DatagridList.DataBind()
reader.Close()
ds.ReadXml(reader)<—- here i dont know what to write,error is coming here
hello,
i finally come up with this solution.. may be it will be usefull for someone who’s requirement is like this:
on server side code:
‘–generate xml file in web server
ds.WriteXml(Server.MapPath(”../serverApp/files/data.xml”))
‘—XXXXXX—-
on client side code:
Imports System.IO
Imports System.Text
Imports System.Net
Imports System.Xml
Dim ds As New DataSet
Dim objResponse As HttpWebResponse
Dim url As string= “http://xxx.xxx.xxx.xxx/serverApp/files/data.xml”
Dim Myuri As New Uri(url)
Dim objRequest As WebRequest = WebRequest.Create(Myuri)
Dim objStreamReader As StreamReader
Dim m_nodelist As XmlNodeList
Dim m_node As XmlNode
Dim SB As New StringBuilder
Dim XW As New XmlTextWriter(New StringWriter(SB))
Dim xmlRepDoc As New XmlDocument
objResponse = CType(objRequest.GetResponse, HttpWebResponse)
objStreamReader = New StreamReader(objResponse.GetResponseStream())
xmlRepDoc.Load(objStreamReader)
‘–Convert XmlDocument to Dataset
xmlRepDoc.WriteTo(XW)
XW.Close()
ds.ReadXml(New StringReader(SB.ToString))
Datagrid1.DataSource = ds
Datagrid1.DataBind()
‘—-XXXXX—–
‘–this is if we want particular value of a item from XmlDocument..
m_nodelist = xmlRepDoc.SelectNodes(”/NewDataSet/subitem”)
For Each m_node In m_nodelist
TextBox1.Text = m_node.ChildNodes.Item(0).InnerText
Next
‘—–XXXXX—-
thanks Prashant for giving the hint..Really appreciate it!!
Hi Saurabh,
“ds.ReadXml(reader)<—- here i dont know what to write,error is coming here”
for the above question, in place of reader u nee dto write the XML file name which you want to read. If you are creating a file dynamically, then i suppose it will be something like :
ds.ReadXML(”www.mydomain.com/myxmlpage.aspx”)
Try this if you got solved. Else if you solved then all credit goes to you dude, caz you worked on that….
Thanks,
Prashant
How to make a page redirection till server completes the response
Excellent!!!!!!!
Thanks, It’s a nice article
Thank you very much.In detail explanation .Thank you once again.
Explained very nicely. Thanks a ton.
Hi Prashant Pandey,
Very good explanation for server.transfer Vs Response .Redirect(). Thank You for the great explanation.Its really excellent.
Itz very nice explanation.. Thankz lot…
Very clearly explained….
Thank u so much:)
hi,
nice article.
view this website
http://www.tipscentre.net
Hi
It was very informative. I was always confused by these terms and looking forward for more such informative articles.
Thank you
sriniavs
Hey ! Thanks a lot … this is very useful info !
Well explained
Hi,
Excellent work!!! Explained very clearly…
Hi,
Excellent work!!! Explained very clearly…
well but not clear how to transfer variables values from one page to other…………….thanks for nice diff…….
Hi,
Thank u so much. You Explanation is very clear.
hi
with the help of u r explanation clear my all confusion
thanks
hi
its an wonderful article…u explained in its right way!!
thanks a lot
Very nice article with very quality information
Nice presentation and very handy… Thank you!
Hey Guys,
Really Excellent.
Thanks.
Raj Aryan
Excellent machuuuuuuu!!!!!!!11
Excellent Machu!!!!!!!!
Nice article. content is presented with more clarity. thanks a lot
boss really u hv cleared my doubt.
thanks
vinit dhama
Many thanks
good
The only difference between Server.Tranfer/Server.Execute and Response.Redirect is that HttpApplication is reused in the first case and in the later case the request comes from a browser as a new request.