Server.Transfer VS Response.Redirect – Simplified

https://www.codeproject.com/Articles/775221/Server-Transfer-VS-Response-Redirect-Simplified

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 for Server.Transfer VS Response.Redirect – Simplified

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.

 

 

“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 for Server.Transfer VS Response.Redirect – Simplified

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 for Server.Transfer VS Response.Redirect – Simplified

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 for Server.Transfer VS Response.Redirect – Simplified

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

 Server.TransferResponse.Redirect
RedirectionRedirection is done by the server.Redirection is done by the browser client.
Browser URLDoes not change.Changes to the redirected target page.
When to useRedirect 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.

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 for Server.Transfer VS Response.Redirect – Simplified

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.

 

转载于:https://www.cnblogs.com/chucklu/p/10757743.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值