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

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


当前回答

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

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

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

其他回答

更多关于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 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

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.

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

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

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

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

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

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

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

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