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

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


当前回答

简而言之:回应。重定向只是告诉浏览器访问另一个页面。服务器。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").

其他回答

响应。重定向只是向浏览器发送一条消息(HTTP 302)。

服务器。传输在浏览器一无所知的情况下发生,浏览器请求一个页面,但服务器返回另一个页面的内容。

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

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

响应。重定向的代价更大,因为它向服务器添加了额外的行程,以确定要去哪里。

服务器。传输是更有效的,但它可能会误导用户,因为Url没有物理改变。

根据我的经验,性能上的差异还没有大到可以使用后一种方法

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

我们希望将请求重定向到我们服务器上的一些普通HTML页面或其他web服务器 我们不关心在每个请求上引起到服务器的额外往返 我们不需要保留原始请求中的查询字符串和表单变量 我们希望我们的用户能够看到新的重定向URL,他在他的浏览器重定向(并能够收藏它,如果它的必要)

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

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

服务器之美。转移是你可以用它做的:

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

只要使用Server,就可以使用上述方法从上一页获取任何内容。转移但不响应。重定向