服务器之间的区别是什么。转移和响应。重定向?

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


当前回答

服务器。传输不会改变客户端浏览器中的URL,因此实际上浏览器并不知道您更改了另一个服务器端处理程序。响应。重定向告诉浏览器移动到另一个页面,因此标题栏中的url会发生变化。

服务器。传输稍微快一点,因为它避免了到服务器的一次往返,但不更改url可能对您有好处,也可能有坏处,这取决于您想要做什么。

其他回答

服务器。传输不会改变客户端浏览器中的URL,因此实际上浏览器并不知道您更改了另一个服务器端处理程序。响应。重定向告诉浏览器移动到另一个页面,因此标题栏中的url会发生变化。

服务器。传输稍微快一点,因为它避免了到服务器的一次往返,但不更改url可能对您有好处,也可能有坏处,这取决于您想要做什么。

简而言之:回应。重定向只是告诉浏览器访问另一个页面。服务器。Transfer有助于减少服务器请求,保持URL相同,并且通过一些错误处理,允许您传输查询字符串和表单变量。

我发现并同意以下观点:

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

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()不会更改地址栏。你不能反击。

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

否则,我将总是使用Response.Redirect()。

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

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

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