服务器传输与服务器传输 响应.重定向

发布于 2024-07-07 11:36:51 字数 161 浏览 13 评论 0原文

Server.TransferResponse.Redirect 之间有什么区别?

  • 各自的优点和缺点是什么?
  • 什么时候其中一种比另一种更合适?
  • 什么时候不合适?

What is difference between Server.Transfer and Response.Redirect?

  • What are advantages and disadvantages of each?
  • When is one appropriate over the other?
  • When is one not appropriate?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

羁拥 2024-07-14 11:36:52

在此处输入图像描述

"response.redirect" 和 "server.transfer" 有助于将用户从一个页面转移到另一页面,同时该页面正在执行。 但他们进行转移/重定向的方式非常不同。

如果您是视觉爱好者并且希望看到演示而不是理论,我建议您观看下面的 Facebook 视频,该视频以更具演示性的方式解释了其中的差异。

https://www.facebook.com/photo.php?v=762186150488997

它们之间的主要区别在于谁进行转移。 在“response.redirect”中,传输由浏览器完成,而在“server.transfer”中,传输由服务器完成。 让我们尝试更详细地理解这一说法。

在“Server.Transfer”中,以下是传输发生的顺序:-

1. 用户向 ASP.NET 页面发送请求。 在下图中,请求被发送到“WebForm1”,我们希望导航到“Webform2”。

2.服务器开始执行“Webform1”,页面的生命周期开始。 但在页面的完整生命周期完成之前,“Server.transfer”发生在“WebForm2”上。

3.创建“Webform2”页面对象,执行完整页面生命周期,然后将输出 HTML 响应发送到浏览器。

在此处输入图像描述

在“Response.Redirect”中,以下是导航事件的顺序:-

1.Client(浏览器) ) 向页面发送请求。 在下图中,请求被发送到“WebForm1”,我们希望导航到“Webform2”。

2.“Webform1”的生命周期开始执行。 但在生命周期之间“Response.Redirect”发生。

3.现在,他不再由服务器进行重定向,而是向浏览器发送 HTTP 302 命令。 该命令告诉浏览器他必须向“Webform2.aspx”页面发起 GET 请求。

4.浏览器解释302命令并发送“Webform2.aspx”的GET请求。

在此处输入图像描述

换句话说,“Server.Transfer”由服务器执行,而“Response.Redirect”由服务器执行thr浏览器。 “Response.Redirect”需要两个请求才能进行页面重定向。

那么何时使用“Server.Transfer”以及何时使用“Response.Redirect”?

当您想要导航驻留在同一服务器上的页面时,请使用“Response.Redirect” “当您想要在驻留在不同服务器和域上的页面之间导航时。

在此处输入图像描述

下面是一个汇总表,其中列出了差异以及使用场景。

在此处输入图像描述

enter image description here

"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 case you are visual guy and would like see demonstration rather than theory I would suggest to see the below facebook video which explains the difference in a more demonstrative way.

https://www.facebook.com/photo.php?v=762186150488997

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 detail manner.

In "Server.Transfer" following is the sequence of how 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.

enter image description here

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 of 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".

enter image description here

In other words "Server.Transfer" is executed by the server while "Response.Redirect" is executed by thr browser. "Response.Redirect" needs to 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.

enter image description here

Below is a summary table of which chalks out differences and in which scenario to use.

enter image description here

你没皮卡萌 2024-07-14 11:36:52

Server.Transfer 的美妙之处在于您可以用它做的事情:

TextBox myTxt = (TextBox)this.Page.PreviousPage.FindControl("TextBoxID");

只要您使用 Server.Transfer 而不是 Response.Redirect,您就可以使用上述方法从上一页获取任何内容

The beauty of Server.Transfer is what you can do with it:

TextBox myTxt = (TextBox)this.Page.PreviousPage.FindControl("TextBoxID");

You can get anything from your previous page using the above method as long as you use Server.Transfer but not Response.Redirect

注定孤独终老 2024-07-14 11:36:52

除了ScarletGarden的评论之外,您还需要考虑搜索引擎和您的重定向的影响。 此页面已永久移动吗? 暂时地? 这很重要。

请参阅:Response.Redirect 与“301 永久移动”

我们都使用过 Response.Redirect
一次或另一次。 这就是快
以及吸引访客指出的简单方法
如果他们以某种方式朝着正确的方向前进
最终在错误的地方。 但你有没有
知道 Response.Redirect 发送一个
HTTP 响应状态码“302
当你真正想要的时候找到了”
发送“301永久移动”?

区别看似很小,但实际上
在某些情况下它实际上可以使
巨大差距。 例如,如果您
使用“301 永久移动”响应
代码,大多数搜索引擎都会删除
索引中的过时链接以及
将其更换为新的。 如果你
使用“302 Found”,他们会继续
返回旧页面...

In addition to ScarletGarden's comment, you also need to consider the impact of search engines and your redirect. Has this page moved permanently? Temporarily? It makes a difference.

see: Response.Redirect vs. "301 Moved Permanently":

We've all used Response.Redirect at
one time or another. It's the quick
and easy way to get visitors pointed
in the right direction if they somehow
end up in the wrong place. But did you
know that Response.Redirect sends an
HTTP response status code of "302
Found" when you might really want to
send "301 Moved Permanently"?

The distinction seems small, but in
certain cases it can actually make a
big difference. For example, if you
use a "301 Moved Permanently" response
code, most search engines will remove
the outdated link from their index and
replace it with the new one. If you
use "302 Found", they'll continue
returning to the old page...

绿光 2024-07-14 11:36:52

如上所述,存在许多差异。 除了以上几点之外,还有一处不同。 Response.Redirect() 可用于将用户重定向到不属于应用程序一部分的任何页面,但 Server.Transfer() 只能用于在应用程序内重定向用户应用。

//This will work.
Response.Redirect("http://www.google.com");

//This will not work.
Server.Transfer("http://www.google.com");

There are many differences as specified above. Apart from above all, there is one more difference. Response.Redirect() can be used to redirect user to any page which is not part of the application but Server.Transfer() can only be used to redirect user within the application.

//This will work.
Response.Redirect("http://www.google.com");

//This will not work.
Server.Transfer("http://www.google.com");
-小熊_ 2024-07-14 11:36:52

传输完全在服务器端进行。 客户端地址栏保持不变。 请求之间的上下文传输有些复杂。 刷新和重新启动页面处理程序可能会很昂贵,因此请尽早在管道中进行传输,例如在 BeginRequest 期间在 HttpModule 中进行传输。 仔细阅读 MSDN 文档,并测试和理解 HttpContext.Request 的新值 - 特别是在回发场景中。 我们通常使用 Server.Transfer 来处理错误场景。

重定向以 302 状态终止请求,客户端往返响应并在内部发生异常(较小的服务器性能命中 - 取决于您每天执行的次数),然后客户端导航到新地址。 浏览器地址栏& 历史记录更新等。客户支付额外往返的费用 - 费用根据延迟而变化。 在我们的业务中,我们经常重定向,我们编写了自己的模块来避免异常成本。

Transfer is entirely server-side. Client address bar stays constant. Some complexity about the transfer of context between requests. Flushing and restarting page handlers can be expensive so do your transfer early in the pipeline e.g. in an HttpModule during BeginRequest. Read the MSDN docs carefully, and test and understand the new values of HttpContext.Request - especially in Postback scenarios. We usually use Server.Transfer for error scenarios.

Redirect terminates the request with a 302 status and client-side roundtrip response with and internally eats an exception (minor server perf hit - depends how many you do a day) Client then navigates to new address. Browser address bar & history updates etc. Client pays the cost of an extra roundtrip - cost varies depending on latency. In our business we redirect a lot we wrote our own module to avoid the exception cost.

喵星人汪星人 2024-07-14 11:36:52

Response.Redirect 的成本更高,因为它增加了服务器的额外行程来确定要去哪里。

Server.Transfer 效率更高,但可能会有点误导用户,因为 Url 不会发生物理变化。

根据我的经验,性能差异还不足以使用后一种方法

Response.Redirect is more costly since it adds an extra trip to the server to figure out where to go.

Server.Transfer is more efficient however it can be a little mis-leading to the user since the Url doesn't physically change.

In my experience, the difference in performance has not been significant enough to use the latter approach

信仰 2024-07-14 11:36:52

Server.Transfer 不会更改客户端浏览器中的 URL,因此浏览器实际上并不知道您更改为另一个服务器端处理程序。 Response.Redirect 告诉浏览器移动到不同的页面,因此标题栏中的 url 会发生变化。

Server.Transfer 稍微快一些,因为它避免了到服务器的一次往返,但不更改 url 对您来说可能是好事,也可能是坏事,具体取决于您想要做什么。

Server.Transfer doesn't change the URL in the client browser, so effectively the browser does not know you changed to another server-side handler. Response.Redirect tells the browser to move to a different page, so the url in the titlebar changes.

Server.Transfer is slightly faster since it avoids one roundtrip to the server, but the non-change of url may be either good or bad for you, depending on what you're trying to do.

清君侧 2024-07-14 11:36:52

Response.Redirect:告诉浏览器可以在新位置找到所请求的页面。 然后,浏览器向新页面发起另一个请求,在浏览器中加载其内容。 这会导致浏览器发出两个请求。

Server.Transfer:它将执行从服务器上的第一页转移到第二页。 就浏览器客户端而言,它发出了一个请求,并且初始页面是响应内容的页面。
这种方法的好处是减少了从客户端浏览器到服务器的一次往返。 此外,任何发布的表单变量和查询字符串参数也可用于第二页。

Response.Redirect: tells the browser that the requested page can be found at a new location. The browser then initiates another request to the new page loading its contents in the browser. This results in two requests by the browser.

Server.Transfer: It transfers execution from the first page to the second page on the server. As far as the browser client is concerned, it made one request and the initial page is the one responding with content.
The benefit of this approach is one less round trip to the server from the client browser. Also, any posted form variables and query string parameters are available to the second page as well.

逆流 2024-07-14 11:36:52

关于 Transfer() 的更多细节,它实际上是 Server.Execute() + Response.End(),其源代码如下(来自 Mono/.net 4.0):

public void Transfer (string path, bool preserveForm)
{
    this.Execute (path, null, preserveForm, true);
    this.context.Response.End ();
}

对于 Execute(),它运行的是给定路径的处理程序,请参阅

ASP.NET 不会验证当前用户是否有权查看 Execute 方法传递的资源。 尽管 ASP.NET 授权和身份验证逻辑在调用原始资源处理程序之前运行,但 ASP.NET 直接调用 Execute 方法指示的处理程序,并且不会为新资源重新运行身份验证和授权逻辑。 如果您的应用程序的安全策略要求客户端拥有适当的授权才能访问资源,则应用程序应强制重新授权或提供自定义访问控制机制。

您可以使用Redirect 方法而不是Execute 方法强制重新授权。 Redirect 执行客户端重定向,其中浏览器请求新资源。 由于此重定向是进入系统的新请求,因此它受到 Internet 信息服务 (IIS) 和 ASP.NET 安全策略的所有身份验证和授权逻辑的约束。

-来自 MSDN

Just more details about Transfer(), it's actually is Server.Execute() + Response.End(), its source code is below (from Mono/.net 4.0):

public void Transfer (string path, bool preserveForm)
{
    this.Execute (path, null, preserveForm, true);
    this.context.Response.End ();
}

and for Execute(), what it is to run is the handler of the given path, see

ASP.NET does not verify that the current user is authorized to view the resource delivered by the Execute 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 Execute method and does not rerun authentication and authorization logic for the new resource. If your application's security policy requires clients to have appropriate 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 Execute method. Redirect 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 Internet Information Services (IIS) and ASP.NET security policy.

-from MSDN

不一样的天空 2024-07-14 11:36:52

Response.Redirect 涉及额外的往返并更新地址栏。

Server.Transfer 不会导致地址栏发生更改,服务器会使用另一个页面的内容响应请求,

例如

Response.Redirect:-

  1. 在客户端上,浏览器请求页面 http://InitiallyRequestedPage.aspx
  2. 在服务器上响应302请求,传递重定向地址http://AnotherPage.aspx
  3. 在客户端上,浏览器向地址 http://AnotherPage.aspx 发出第二个请求。
  4. 在服务器上响应 http://AnotherPage.aspx

Server.Transfer:-

  1. 的 内容客户端浏览器请求一个页面 http://InitiallyRequestedPage.aspx
  2. 到服务器 Server.Transfer 到 http://AnotherPage.aspx
  3. 在服务器上,对 http://InitiallyRequestedPage.aspxhttp://AnotherPage.aspx 传回内容

Response.Redirect

优点:-
RESTful - 它改变地址栏,该地址可用于记录请求之间的状态变化。

缺点:-
慢 - 客户端和服务器之间有额外的往返。 当客户端和服务器之间存在大量延迟时,这可能会很昂贵。

Server.Transfer

优点:-
快的。

缺点:-
状态丢失 - 如果您使用 Server.Transfer 更改应用程序的状态以响应回发,如果随后重新加载页面,则该状态将丢失,因为地址栏将与第一个页面相同要求。

Response.Redirect involves an extra round trip and updates the address bar.

Server.Transfer does not cause the address bar to change, the server responds to the request with content from another page

e.g.

Response.Redirect:-

  1. On the client the browser requests a page http://InitiallyRequestedPage.aspx
  2. On the server responds to the request with 302 passing the redirect address http://AnotherPage.aspx.
  3. On the client the browser makes a second request to the address http://AnotherPage.aspx.
  4. On the server responds with content from http://AnotherPage.aspx

Server.Transfer:-

  1. On the client browser requests a page http://InitiallyRequestedPage.aspx
  2. On the server Server.Transfer to http://AnotherPage.aspx
  3. On the server the response is made to the request for http://InitiallyRequestedPage.aspx passing back content from http://AnotherPage.aspx

Response.Redirect

Pros:-
RESTful - It changes the address bar, the address can be used to record changes of state inbetween requests.

Cons:-
Slow - There is an extra round-trip between the client and server. This can be expensive when there is substantial latency between the client and the server.

Server.Transfer

Pros:-
Quick.

Cons:-
State lost - If you're using Server.Transfer to change the state of the application in response to post backs, if the page is then reloaded that state will be lost, as the address bar will be the same as it was on the first request.

娇柔作态 2024-07-14 11:36:52

响应.重定向
Response.Redirect() 会将您发送到一个新页面,更新地址栏并将其添加到浏览器历史记录中。 在浏览器上,您可以单击“返回”。
它将请求重定向到我们服务器上的一些纯 HTML 页面或其他 Web 服务器。
它会导致每个请求与服务器进行额外的往返。
它不会保留原始请求中的查询字符串和表单变量。
它可以在浏览器中重定向的位置查看新的重定向 URL(如果需要,还可以将其添加为书签)。
回复。 重定向只是将消息发送到 (HTTP 302) 浏览器。

服务器传输
Server.Transfer() 不会更改地址栏,我们无法反击。当他/她不希望用户看到他要去哪里时,应该使用 Server.Transfer()。 有时在“正在加载”类型的页面上。
它将当前页面请求传输到同一服务器上的另一个 .aspx 页面。
它保留服务器资源并避免不必要的服务器往返。
它保留查询字符串和表单变量(可选)。
它不会显示在用户 Web 浏览器中重定向请求的真实 URL。
Server.Transfer 在浏览器不知道任何情况下发生,浏览器请求一个页面,但服务器返回另一个页面的内容。

Response.Redirect
Response.Redirect() will send you to a new page, update the address bar and add it to the Browser History. On your browser you can click back.
It redirects the request to some plain HTML pages on our server or to some other web server.
It causes additional roundtrips to the server on each request.
It doesn’t preserve Query String and Form Variables from the original request.
It enables to see the new redirected URL where it is redirected in the browser (and be able to bookmark it if it’s necessary).
Response. Redirect simply sends a message down to the (HTTP 302) browser.

Server.Transfer
Server.Transfer() does not change the address bar, we cannot hit back.One should use Server.Transfer() when he/she doesn’t want the user to see where he is going. Sometime on a "loading" type page.
It transfers current page request to another .aspx page on the same server.
It preserves server resources and avoids the unnecessary roundtrips to the server.
It preserves Query String and Form Variables (optionally).
It doesn’t show the real URL where it redirects the request in the users Web Browser.
Server.Transfer happens without the browser knowing anything, the browser request a page, but the server returns the content of another.

七颜 2024-07-14 11:36:51

Response.Redirect 只是将消息 (HTTP 302) 发送到浏览器。

Server.Transfer 在浏览器不知道任何情况下发生,浏览器请求一个页面,但服务器返回另一个页面的内容。

Response.Redirect simply sends a message (HTTP 302) down to the browser.

Server.Transfer happens without the browser knowing anything, the browser request a page, but the server returns the content of another.

方圜几里 2024-07-14 11:36:51

Response.Redirect() 会将您发送到一个新页面,更新地址栏并将其添加到浏览器历史记录中。 在浏览器上,您可以单击“返回”。

Server.Transfer() 不会更改地址栏。 你无法反击。

当我不希望用户看到我要去哪里时,我使用Server.Transfer()。 有时在“正在加载”类型的页面上。

否则我将始终使用 Response.Redirect()。

Response.Redirect() will send you to a new page, update the address bar and add it to the Browser History. On your browser you can click back.

Server.Transfer() does not change the address bar. You cannot hit back.

I use Server.Transfer() when I don't want the user to see where I am going. Sometimes on a "loading" type page.

Otherwise I'll always use Response.Redirect().

难得心□动 2024-07-14 11:36:51

简而言之:Response.Redirect 只是告诉浏览器访问另一个页面。 Server.Transfer 有助于减少服务器请求,保持 URL 相同,并且通过一些 bug 修复,允许您传输查询字符串和表单变量。

我发现并同意的东西(来源):

Server.Transfer 类似,它将用户发送到另一个页面
使用诸如 Server.Transfer("WebForm2.aspx") 之类的语句。 然而,
该声明有许多明显的优点和缺点。

首先,使用Server.Transfer转移到另一个页面
节省服务器资源。 而不是告诉浏览器
重定向,它只是改变 Web 服务器上的“焦点”
传输请求。 这意味着您不会获得尽可能多的 HTTP
请求通过,从而减轻您的压力
Web 服务器并使您的应用程序运行得更快。

但请注意:因为“转移”过程只能对那些
在服务器上运行的站点; 您不能使用 Server.Transfer 发送
用户访问外部站点。 只有 Response.Redirect 可以做到这一点。

其次,Server.Transfer 在浏览器中维护原始 URL。
这确实可以帮助简化数据输入技术,尽管它可能
调试时会造成混乱。

这还不是全部:Server.Transfer 方法还有第二个
参数—“preserveForm”。 如果将其设置为 True,则使用语句
Server.Transfer("WebForm2.aspx", True),现有查询
字符串和任何表单变量仍然可用于您的页面
正在转移到。

例如,如果您的 WebForm1.aspx 有一个名为的 TextBox 控件
TextBox1 和您使用preserveForm 转移到WebForm2.aspx
参数设置为 True,您将能够检索
通过引用原始页面的TextBox控件
Request.Form("TextBox1").

To be Short: Response.Redirect simply tells the browser to visit another page. Server.Transfer helps reduce server requests, keeps the URL the same and, with a little bug-bashing, allows you to transfer the query string and form variables.

Something I found and agree with (source):

Server.Transfer is similar in that it sends the user to another page
with a statement such as Server.Transfer("WebForm2.aspx"). However,
the statement has a number of distinct advantages and disadvantages.

Firstly, 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.

But watch out: because the "transfer" process can work on only those
sites running on the server; you can't use Server.Transfer to send
the user to an external site. Only Response.Redirect can do that.

Secondly, Server.Transfer maintains the original URL in the browser.
This can really help streamline data entry techniques, although it may
make for confusion when debugging.

That's not all: The Server.Transfer method also has a second
parameter—"preserveForm". If you set this to True, using a statement
such as Server.Transfer("WebForm2.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 WebForm1.aspx has a TextBox control called
TextBox1 and you transferred to WebForm2.aspx with the preserveForm
parameter set to True, you'd be able to retrieve the value of the
original page TextBox control by referencing
Request.Form("TextBox1").

小伙你站住 2024-07-14 11:36:51

Response.Redirect() 应该在以下情况下使用:

  • 我们想要将请求重定向到我们服务器上的一些纯 HTML 页面,或者重定向到
  • 我们不关心导致到服务器的额外往返的 其他 Web 服务器。每个请求
  • 我们不需要保留原始请求中的查询字符串和表单变量,
  • 我们希望我们的用户能够在浏览器中看到新的重定向 URL(并且能够在必要时将其添加为书签)

Server.Transfer() 应该在以下情况下使用:

  • 我们想要将当前页面请求传输到同一服务器上的另一个 .aspx 页面
  • 服务器进行不必要的往返
  • 我们想要保留服务器资源并避免与我们想要保留查询的 字符串和表单变量(可选),
  • 我们不需要在用户 Web 浏览器中显示我们重定向请求的真实 URL

Response.Redirect() should be used when:

  • we want to redirect the request to some plain HTML pages on our server or to some other web server
  • we don't care about causing additional roundtrips to the server on each request
  • we do not need to preserve Query String and Form Variables from the original request
  • we want our users to be able to see the new redirected URL where he is redirected in his browser (and be able to bookmark it if its necessary)

Server.Transfer() should be used when:

  • we want to transfer current page request to another .aspx page on the same server
  • we want to preserve server resources and avoid the unnecessary roundtrips to the server
  • we want to preserve Query String and Form Variables (optionally)
  • we don't need to show the real URL where we redirected the request in the users Web Browser
绾颜 2024-07-14 11:36:51

Response.Redirect 在第一页到达客户端之后将页面重定向到另一个页面。 所以客户端知道重定向。

Server.Transfer 退出页面的当前执行。 客户端不知道重定向。 它允许您传输查询字符串和表单变量。

所以还是根据自己的需求来选择哪个更好。

Response.Redirect redirects page to another page after first page arrives to client. So client knows the redirection.

Server.Transfer quits current execution of the page. Client does not know the redirection. It allows you to transfer the query string and form variables.

So it depends to your needs to choose which is better.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文