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

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


当前回答

简而言之:回应。重定向只是告诉浏览器访问另一个页面。服务器。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()会将您发送到一个新页面,更新地址栏并将其添加到浏览器历史记录中。在浏览器上,您可以单击返回。

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

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

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

响应。重定向涉及一个额外的往返和更新地址栏。

服务器。传输不会导致地址栏更改,服务器会用来自另一个页面的内容响应请求

e.g.

响应。重定向:

在客户机上,浏览器请求一个页面http://InitiallyRequestedPage.aspx 在服务器上响应请求,302传递重定向地址http://AnotherPage.aspx。 在客户机上,浏览器向地址http://AnotherPage.aspx发出第二个请求。 在服务器上响应来自http://AnotherPage.aspx的内容

服务器。转让:

在客户机上,浏览器请求一个页面http://InitiallyRequestedPage.aspx 在服务器上。转至http://AnotherPage.aspx 在服务器上,对http://InitiallyRequestedPage.aspx的请求做出响应,从http://AnotherPage.aspx返回内容

响应。重定向

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

缺点:- 慢——在客户端和服务器之间有一个额外的往返。当客户端和服务器之间存在大量延迟时,这样做的代价可能会很高。

服务器。转移

优点:- 快。

缺点:- 状态丢失-如果您正在使用服务器。转移来改变应用程序的状态以响应回贴,如果页面随后被重新加载,该状态将丢失,因为地址栏将与第一次请求时相同。

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

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

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

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