Introduction:
There
are two ways to go to other page from one page in asp.net.
. ‘Server. Transfer’ and ‘Response. Redirect’ both are used for the same purpose i.e. to transfer from one page to another page .
. ‘Server. Transfer’ and ‘Response. Redirect’ both are used for the same purpose i.e. to transfer from one page to another page .
But there is difference in
these two ways to redirect to other page. In this article I’ll explain it
in detail.
Response.Redirect vs
Server.Transfer
1.
Extra Round Trip: ‘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 it redirects the user directly from the server itself. So in case of
‘Server. Transfer’ there is no round trip while Response. Redirect has a round
trip and hence puts extra load on server and take extra time.
2.
Cross Server Redirection: Using ‘Server. Transfer’ we cannot
redirect to external websites or website pages. E.g. if your website is www.XYZ.com then you move to www.google.com by using ‘Server. Transfer’, but
you can move to internal pages of www.XYZ.com,
i.e. within the websites. Cross server redirection is possible only by using
‘Response.Redirect’ i.e. it allows redirection to internal as well as external
websites and website pages.
3.
With
‘Response. Redirect’ we can redirect the user to the both type of pages .html
or .aspx e.g. Response. Redirect (“OtherPage.html”) OR Response. Redirect
(“OtherPage.aspx”) But in case of ‘Server. Transfer’ we can redirect user to
.asp or .aspx pages only e.g. Server. Transfer (“OtherPage.asp”) OR Server.
Transfer (“OtherPage.aspx”) not to Server. Transfer (“OtherPage.html”).
4.
URL Change:
In ‘Server. Transfer’ URL doesn’t change but in case of ‘Response.
Redirect’ URL changes.
5.
For security Reason: When we want to allow our website’s
URL can be copied then ‘Response. Redirect’ is better but for security reasons
‘Server. Transfer’ is better because URL cannot be copied.
6.
Server Or Client Process:
‘Server. Transfer’ is a server process whereas ‘Response. Redirect’ is a
client process.
7.
‘Server.Transfer’ preserves Query
String and Form Variables (optionally). ‘Response. Redirect’ doesn’t preserve
Query String and Form Variables from the original request.
8.
Speed: ‘Server.
Transfer’ is faster since there is one less round trip as compared to
‘Response. Redirect’. Transferring to another page using ’Server.
Transfer’ conserves server resources. Instead of telling the browser to
redirect, it simply changes the focus on the Web server and transfers the
request. This means you don't get quite as many HTTP requests coming through,
which therefore eases the pressure on your Web server and makes your
applications run faster.
9.
‘Server. Transfer’ allow us to directly
access the values, controls and properties of the previous page which we can’t
do with ‘Response. Redirect’. The ’Server. Transfer’ method also has a
second parameter—"preserveForm". If you set this to True, using a
statement such as Server. Transfer ("OtherPage.aspx", True), the
existing query string and any form variables will still be available to the
page you are transferring to. For example, if your CurrentPage.aspx has a
TextBox control called TextBox1 and you transferred to OtherPage.aspx with the
preserveForm parameter set to True, you'd be able to retrieve the value of the
original page’s TextBox control by referencing Request. Form
("TextBox1").
10. ‘Response.
Redirect’ involves a roundtrip to the server whereas ‘Server. Transfer’
conserves server resources by avoiding the roundtrip. It just changes the focus
of the web server to a different page and transfers the page processing to a
different page. 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 web server only then a 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.
0 comments:
Post a Comment