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”.

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.

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”.

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.

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.

Hide   Copy 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.

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.

Server.Transfer VS Response.Redirect – Simplified的更多相关文章

  1. 1. Server.Transfer和Response.Redirect

    今天在使用ServerTransfer和Response.Redirect定位到当前页面来实现刷新页面时,发现了一些现象: 1.使用Response.Redirect刷新本页面,造成当前页面显示的数据 ...

  2. Server.Transfer 和 Response.Redirect 用法区别

    在ASP.NET中,在后台传值方式目前大多都是用 Response.Redirect("页面地址") 来重定向页面的,但是现在还有一种方式也可以达到重定向页面的作用,而且在某些时刻 ...

  3. Server Transfer()和Response.Redirect()的使用

    一.Server Transfer() Server.Transfer:对于当前请求,终止当前页的执行,并使用指定的页url路径来开始执行一个新页. 1. Server.Transfer只能够转跳到本 ...

  4. Server.Transfer和Response.Redirect区别

    根本上,Response是叫浏览器去重新转向到指定的网页,而Server自然是发生在服务器端为主了,因此会有以下区别:1. Server.Transfer只能够转跳到本地虚拟目录指定的页面,而Resp ...

  5. 页面跳转 Server.Transfer和 Response.Redirect的区别

    1.Server.Transfer 用于把处理的控制权从一个页面转移到另一个页面,在转移的工程中没有离开服务器内部控件(如request,session等)保存的信息不变.因此你能从a页面跳转到b页面 ...

  6. Server.Transfer和Response.Redirect的区别

    (1)Server.Transfer方法: Server.Transfer("m2.aspx");//页面转向(服务器上执行). 服务器停止解析本页,保存此页转向前的数据后,再使页 ...

  7. asp.net知识汇总-页面跳转Server.Transfer和Response.Redirect

    1. Server.Transfer 服务器端跳转 webform1.aspx跳转到webform2.aspx页面 webform1.aspx代码如下: protected void Page_Loa ...

  8. c#页面重定向,Server.Transfer 和 Response.Redirect

    Server.Transfer() 重定向发生在服务器端,把处理的控制权从当前页面转移到另一个页面,在转移的工程中没有离开服务器内部控件(如request,session等)保存的信息不变. 1.只能 ...

  9. ASP.NET页面传值之Server.Transfer 和Response.Direct

    先看实例: B.apsx:         public string TextBox1Text         {             get             {             ...

随机推荐

  1. tensorflow学习笔记————分类MNIST数据集

    在使用tensorflow分类MNIST数据集中,最容易遇到的问题是下载MNIST样本的问题. 一般是通过使用tensorflow内置的函数进行下载和加载, from tensorflow.examp ...

  2. Eclipse的设置、调优、使用(解决启动卡顿等问题)----转

    eclipse调优 一般在不对eclipse进行相关设置的时候,使用eclipse总是会觉得启动好慢,用起来好卡,其实只要对eclipse的相关参数进行一些配置,就会有很大的改善. 加快启动速度 1. ...

  3. iframe 加载外部资源,显示隐藏loading,onload失效

    在项目中使用iframe 来加载外部资源,需要在iframe请求外部资源的时候,需要显示一个loading,在加载完成后,将这个loading隐藏掉,刚开始看到W3C中 iframe有一个 onloa ...

  4. thinkphp 在本地正常,在云端ubuntu下报控制器不存在

    thinkphp 在本地正常,在云端ubuntu下报控制器不存在的错 ubuntu是严格区分大小写的,本地的服务器可能对大小写要求没有那么严格, thinkphp的控制器的文件夹默认是小写字母,如果你 ...

  5. win7 蓝屏信息获取和处理

    一.先说电脑蓝屏原因和解决方法: 1.驱动不对,驱动和硬件不兼容出现的问题,这个直接卸载软件或者重装驱动. 2.内存条有问题或者内存损坏:这个内存条很可能是没插紧,内存损坏的话,换个内存条. 3.病毒 ...

  6. MySQL数据库导出

    因为业务需要,把MySQL查询的数据导出成csv文件,操作在Navicat中完成. 首选用SELECT语句查询数据,然后Navicat的导出,然后选csv,选路径,再加上首栏就可以了

  7. IntelliJ IDEA 注册码激活

    IntelliJ IDEA 注册码激活 方法一:使用激活服务器激活方法 步骤 1.打开注册/激活窗口: 2.选择 License server: 3.填入上述其中一个网址(如:http://idea. ...

  8. js 限制输入框只能输入数字的问题

    常规情况下,input设置为 type=‘number’  可以避免汉字,字符,字母,空格的输入,但是不能避免小减号 以及小键盘的减号-,加号+的输入, 可以考虑 监控 输入框的oninput事件,方 ...

  9. 项目实战02:LVS 实现负载均衡

    目录 实现基于LVS负载均衡集群的电商网站架构 实战一:LVS的NAT模式实现负载均衡 实战二:LVS的DR 模式实现负载均衡 实战三:实现80.443端口都可访问服务,且LVS实现持久连接 实验四: ...

  10. android studio/Intellij IDEA(MAC OSX)中android模拟器无法启动的一种原因

    翻了stackoverflow上好多方式,各种重装重启清理缓存,都没有效果. 后来发现,用IDEA创建的模拟器的路径是 /Users/xirtam/.android/avd 而IDE使用的路径是 AN ...