服务器之间的区别是什么。转移和响应。重定向?
它们各自的优点和缺点是什么? 什么时候一种比另一种更合适? 什么时候不合适?
服务器之间的区别是什么。转移和响应。重定向?
它们各自的优点和缺点是什么? 什么时候一种比另一种更合适? 什么时候不合适?
当前回答
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.
其他回答
如上所述,有许多不同之处。除此之外,还有一个不同。Response.Redirect()可用于将用户重定向到不属于应用程序的任何页面,但Server.Transfer()只能用于在应用程序内重定向用户。
//This will work.
Response.Redirect("http://www.google.com");
//This will not work.
Server.Transfer("http://www.google.com");
服务器之美。转移是你可以用它做的:
TextBox myTxt = (TextBox)this.Page.PreviousPage.FindControl("TextBoxID");
只要使用Server,就可以使用上述方法从上一页获取任何内容。转移但不响应。重定向
Response.Redirect()会将您发送到一个新页面,更新地址栏并将其添加到浏览器历史记录中。在浏览器上,您可以单击返回。
Server.Transfer()不会更改地址栏。你不能反击。
当我不想让用户看到我要去哪里时,我使用Server.Transfer()。有时在“加载”类型的页面上。
否则,我将总是使用Response.Redirect()。
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.
”反应。重定向"和"服务器。“转移”有助于在页面执行时将用户从一个页面转移到另一个页面。但是他们进行这种转移/重定向的方式非常不同。
如果你是视觉爱好者,想看演示而不是理论,我建议你看看下面的facebook视频,它以更直观的方式解释了差异。
https://www.facebook.com/photo.php?v=762186150488997
他们之间的主要区别是谁来转账。在“响应。重定向“传输是由浏览器在服务器中完成的”。“传输”是由服务器完成的。让我们试着以更详细的方式来理解这句话。
在“服务器。转让”以下是转让发生的顺序:-
1.用户向ASP发送请求。网络页面。在下图中,请求被发送到“WebForm1”,我们希望导航到“Webform2”。
2.服务器开始执行“Webform1”,页面的生命周期开始。但是在页面的整个生命周期完成之前。transfer”恰好是“WebForm2”。
3.创建“Webform2”页面对象,执行整个页面生命周期,然后将输出HTML响应发送到浏览器。
在“回应”中。“重定向”下面是导航事件的顺序:-
1.客户端(浏览器)向页面发送请求。在下图中,请求被发送到“WebForm1”,我们希望导航到“Webform2”。
2.“Webform1”的生命周期开始执行。但在生命周期之间。重定向”发生。
3.现在,他向浏览器发送HTTP 302命令,而不是服务器执行重定向。该命令告诉浏览器必须向“Webform2”发起GET请求。aspx页面”。
4.浏览器解释302命令并为“Webform2.aspx”发送一个GET请求。
换句话说就是“服务器”。服务器在响应时执行“传输”。“重定向”由浏览器执行。”反应。重定向”需要两个请求来做页面的重定向。
所以什么时候使用“服务器”。转移”以及何时使用“响应”。重定向”?
使用“服务器。当您想导航位于同一服务器上的页面时,请使用“响应”。重定向"当你想在不同服务器和域上的页面之间导航时。
下面是一个汇总表,其中列出了不同之处以及在哪个场景中使用。