Click here to Skip to main content
15,895,471 members
Articles / Web Development / ASP.NET
Technical Blog
View Original
Stats
Revisions (6)
C#
ASP.NET
.NET
ATL

Server.Transfer VS Response.Redirect – Simplified

Shivprasad koirala
Rate me:
Please Sign up or sign in to vote.
4.91/5 (46 votes)
16 Jun 2021 CPOL4 min read 199.3K   35   13
This blog will discuss the difference between server.transfer and response.redirect.
  • Introduction
  • “Server.Transfer”vs “response.Redirect”
  • So when to use “Server.Transfer” and when to use “Response.Redirect” ?
  • What is importance of “preserveForm” flag in “Server.Transfer”?
  • Response.Redirect(URL,true) vsResponse.Redirect(URL,false) ?

Introduction

In ASP.NET, some of the concepts do the same task but are meant to be used in different scenarios. One such concept which is confusing and most discussed among developers is the difference between “Server.Transfer” and “Response.Redirect”.

Image 1

response.redirect” and “server.transfer” helps to transfer user from one page to other page while the page is executing. But the way they do this transfer / redirect is very different.

In this short blog, we will discuss how they differ and in which scenarios we should use them.

In case you are a visual person and would like see a demonstration rather than theory, I would suggest to see the below Facebook video which explains the difference in a more demonstrative way.

Image 2

“Server.Transfer”vs “response.Redirect”

The main difference between them is who does the transfer. In “response.redirect”, the transfer is done by the browser while in “server.transfer”, it’s done by the server. Let us try to understand this statement in a more detailed manner.

In “Server.Transfer”, following is the sequence of how the transfer happens:

  1. User sends a request to an ASP.NET page. In the below figure, the request is sent to “WebForm1” and we would like to navigate to “Webform2”.
  2. Server starts executing “Webform1” and the life cycle of the page starts. But before the complete life cycle of the page is completed, “Server.transfer” happens to “WebForm2”.
  3. Webform2” page object is created, full page life cycle is executed and output HTML response is then sent to the browser.
Image 3

One important point to note here is the URL is not changed to the target page. If you have sent request from “Webform1.aspx” to redirect to “WebForm2.aspx” on the browser URL, you will still see “WebForm1.aspx”.

While in “Response.Redirect”, following is the sequence of events for navigation:

  1. Client (browser) sends a request to a page. In the below figure, the request is sent to “WebForm1” and we would like to navigate to “Webform2”.
  2. Life cycle of “Webform1” starts executing. But in between the life cycle “Response.Redirect” happens.
  3. Now rather than server doing a redirect, he sends a HTTP 302 command to the browser. This command tells the browser that he has to initiate a GET request to “Webform2.aspx” page.
  4. Browser interprets the 302 command and sends a GET request for “Webform2.aspx”.
Image 4

In this case, you will see the URLs are changed as per redirection. So if you have redirected to “Webform2.aspx”, then on the browser URL, you should see “WebForm2.aspx”.

In other words, “Server.Transfer” is executed by the server while “Response.Redirect” is executed by the browser. “Response.Redirect” needs two requests to do a redirect of the page.

So When to Use “Server.Transfer” and When to Use “Response.Redirect”?

Use “Server.Transfer” when you want to navigate pages which reside on the same server, use “Response.Redirect” when you want to navigate between pages which resides on different server and domain.

Image 5

Below goes the consolidated table with all the differences as discussed at the top.

  Server.Transfer Response.Redirect
Redirection Redirection is done by the server. Redirection is done by the browser client.
Browser URL Does not change. Changes to the redirected target page.
When to use Redirect between pages of the same server. Redirect between pages on different server and domain.

What is the Importance of “preserveForm” Flag in “Server.Transfer”?

Server.Transfer” helps to redirect from one page to other page. If you wish to pass query string and form data of the first page to the target page during this redirection, you need to set “preserveForm” to “true” as shown in the below code.

C#
Server.Transfer("Webform2.aspx",true);

By default, the value of “preserveForm” is “true”.

Response.Redirect(URL,true) vsResponse.Redirect(URL,false) ?

Response.Redirect(URL,false): Client is redirected to a new page and the current page on the server will keep processing ahead.

Response.Redirect(URL,true): Client is redirected to a new page but the processing of the current page is aborted.

Image 6

Below is a Facebook video which demonstrates practically the difference between server.transfervsresponse.redirect. A big thanks to www.questpond.com to allow me to publish this videos for free on Facebook.

Image 7

For Further Reading do watch the below Interview preparation videos and step by step video series.

  • ASP.NET MVC Interview Questions & Answers
  • C# Interview Questions with Answers
  • Angular Interview Questions with Answers
  • Learn Azure step by step.
  • Learn MVC 5 Step by Step
  • MVC Core Step by Step Tutorial
  • SQL Server step by step
  • Session & Viewstate in ASP.NET.

 

CodeProject

哆哆女性网易经中起名字吗日照网站建设价格商丘的企业有哪些电子商务网站应用系统设计现在做seo怎么样北大周易国学博士网站优化黑帽7画字有哪些起名用长沙专业外贸网站建设起个游戏名周公解梦包饺子海外seo推广键词seo优化深圳网站设计公司费用多少钱东圃网站建设大庆网站建设有什么国外设计网站c语言程序设计精品课程网站世界微尘里电视剧在线观看河南宁陵有什么特产湖南卫视高清周公解梦梦见有人追杀我商丘柴油价格是多少新的起名交互设计学习网站怎样进行网站架构优化中国十大起名大师排名五行缺木缺水起名默字起名男小孩起名洪姓淀粉肠小王子日销售额涨超10倍罗斯否认插足凯特王妃婚姻不负春光新的一天从800个哈欠开始有个姐真把千机伞做出来了国产伟哥去年销售近13亿充个话费竟沦为间接洗钱工具重庆警方辟谣“男子杀人焚尸”男子给前妻转账 现任妻子起诉要回春分繁花正当时呼北高速交通事故已致14人死亡杨洋拄拐现身医院月嫂回应掌掴婴儿是在赶虫子男孩疑遭霸凌 家长讨说法被踢出群因自嘲式简历走红的教授更新简介网友建议重庆地铁不准乘客携带菜筐清明节放假3天调休1天郑州一火锅店爆改成麻辣烫店19岁小伙救下5人后溺亡 多方发声两大学生合买彩票中奖一人不认账张家界的山上“长”满了韩国人?单亲妈妈陷入热恋 14岁儿子报警#春分立蛋大挑战#青海通报栏杆断裂小学生跌落住进ICU代拍被何赛飞拿着魔杖追着打315晚会后胖东来又人满为患了当地回应沈阳致3死车祸车主疑毒驾武汉大学樱花即将进入盛花期张立群任西安交通大学校长为江西彩礼“减负”的“试婚人”网友洛杉矶偶遇贾玲倪萍分享减重40斤方法男孩8年未见母亲被告知被遗忘小米汽车超级工厂正式揭幕周杰伦一审败诉网易特朗普谈“凯特王妃P图照”考生莫言也上北大硕士复试名单了妈妈回应孩子在校撞护栏坠楼恒大被罚41.75亿到底怎么缴男子持台球杆殴打2名女店员被抓校方回应护栏损坏小学生课间坠楼外国人感慨凌晨的中国很安全火箭最近9战8胜1负王树国3次鞠躬告别西交大师生房客欠租失踪 房东直发愁萧美琴窜访捷克 外交部回应山西省委原副书记商黎光被逮捕阿根廷将发行1万与2万面值的纸币英国王室又一合照被质疑P图男子被猫抓伤后确诊“猫抓病”

哆哆女性网 XML地图 TXT地图 虚拟主机 SEO 网站制作 网站优化

This article was originally posted at http://computerauthor.blogspot.com/2014/05/servertransfer-vs-responseredirect_2787.html

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Shivprasad koirala
Architect https://www.questpond.com
India India
Do not forget to watch my Learn step by step video series.


Software Architecture and Design Patterns Interview Questions
Azure DevOps Step by Step Tutorial for beginners
Learn Tableau Step by Step for Beginners
Power BI Interview Questions and Answers
Angular tutorial for beginners | Learn Angular Step by step
MSBI Interview Questions and Answers
JavaScript Interview Questions and Answers
C# Interview Questions and Answers
OOPs Interview Questions and Answers
ASP.NET MVC Interview Questions with answers
Angular Interview Questions and Answers
SQL Server Interview Questions and Answers
Learn Azure Step by Step
Learn MVC Core step by step
Learn Design Pattern in 8 hours
C# tutorial for beginners(4 hrs)
Learn MSBI Step by Step
Learn SQL Server in 16 hours series

Comments and Discussions

 
General My vote of 5 Pin
Lucky Vdb17-Jun-21 21:13
professional Lucky Vdb17-Jun-21 21:13 
Question Can we do response.redirect after Response.TransmitFile Pin
Member 1453705421-Jul-19 8:22
Member 1453705421-Jul-19 8:22 
General My vote of 5 Pin
Santhakumar M6-Jan-16 22:32
professional Santhakumar M6-Jan-16 22:32 
General My vote of 4 Pin
Kamlesh_Mj22-Sep-15 4:17
Kamlesh_Mj22-Sep-15 4:17 
Question Consequences on AJAX Toolkit Pin
Member 1065156510-Aug-15 1:04
Member 1065156510-Aug-15 1:04 
Question Server.transfer in MVC Pin
Member 117327181-Jun-15 0:33
Member 117327181-Jun-15 0:33 
General my vote 5 Pin
King Fisher3-Feb-15 6:10
professional King Fisher3-Feb-15 6:10 
Question Good explanation and questions Pin
Ricardo Cantillo15-Nov-14 6:05
Ricardo Cantillo15-Nov-14 6:05 
General My vote of 5 Pin
Gaurav Aroraa27-Oct-14 10:31
professional Gaurav Aroraa27-Oct-14 10:31 
General My vote of 5 Pin
Sibeesh KV28-Sep-14 19:51
professional Sibeesh KV28-Sep-14 19:51 
General Good Explanation Pin
RomKap21-May-14 19:51
RomKap21-May-14 19:51 
General Nicely explained Pin
sanjay bathre20-May-14 20:56
sanjay bathre20-May-14 20:56 
Question Nice, Simple & Informative Pin
poiuytrewq1234987654320-May-14 20:44
poiuytrewq1234987654320-May-14 20:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


哆哆女性网易经中起名字吗日照网站建设价格商丘的企业有哪些电子商务网站应用系统设计现在做seo怎么样北大周易国学博士网站优化黑帽7画字有哪些起名用长沙专业外贸网站建设起个游戏名周公解梦包饺子海外seo推广键词seo优化深圳网站设计公司费用多少钱东圃网站建设大庆网站建设有什么国外设计网站c语言程序设计精品课程网站世界微尘里电视剧在线观看河南宁陵有什么特产湖南卫视高清周公解梦梦见有人追杀我商丘柴油价格是多少新的起名交互设计学习网站怎样进行网站架构优化中国十大起名大师排名五行缺木缺水起名默字起名男小孩起名洪姓淀粉肠小王子日销售额涨超10倍罗斯否认插足凯特王妃婚姻不负春光新的一天从800个哈欠开始有个姐真把千机伞做出来了国产伟哥去年销售近13亿充个话费竟沦为间接洗钱工具重庆警方辟谣“男子杀人焚尸”男子给前妻转账 现任妻子起诉要回春分繁花正当时呼北高速交通事故已致14人死亡杨洋拄拐现身医院月嫂回应掌掴婴儿是在赶虫子男孩疑遭霸凌 家长讨说法被踢出群因自嘲式简历走红的教授更新简介网友建议重庆地铁不准乘客携带菜筐清明节放假3天调休1天郑州一火锅店爆改成麻辣烫店19岁小伙救下5人后溺亡 多方发声两大学生合买彩票中奖一人不认账张家界的山上“长”满了韩国人?单亲妈妈陷入热恋 14岁儿子报警#春分立蛋大挑战#青海通报栏杆断裂小学生跌落住进ICU代拍被何赛飞拿着魔杖追着打315晚会后胖东来又人满为患了当地回应沈阳致3死车祸车主疑毒驾武汉大学樱花即将进入盛花期张立群任西安交通大学校长为江西彩礼“减负”的“试婚人”网友洛杉矶偶遇贾玲倪萍分享减重40斤方法男孩8年未见母亲被告知被遗忘小米汽车超级工厂正式揭幕周杰伦一审败诉网易特朗普谈“凯特王妃P图照”考生莫言也上北大硕士复试名单了妈妈回应孩子在校撞护栏坠楼恒大被罚41.75亿到底怎么缴男子持台球杆殴打2名女店员被抓校方回应护栏损坏小学生课间坠楼外国人感慨凌晨的中国很安全火箭最近9战8胜1负王树国3次鞠躬告别西交大师生房客欠租失踪 房东直发愁萧美琴窜访捷克 外交部回应山西省委原副书记商黎光被逮捕阿根廷将发行1万与2万面值的纸币英国王室又一合照被质疑P图男子被猫抓伤后确诊“猫抓病”

哆哆女性网 XML地图 TXT地图 虚拟主机 SEO 网站制作 网站优化