服务器之间的区别是什么。转移和响应。重定向?
它们各自的优点和缺点是什么? 什么时候一种比另一种更合适? 什么时候不合适?
服务器之间的区别是什么。转移和响应。重定向?
它们各自的优点和缺点是什么? 什么时候一种比另一种更合适? 什么时候不合适?
当前回答
如上所述,有许多不同之处。除此之外,还有一个不同。Response.Redirect()可用于将用户重定向到不属于应用程序的任何页面,但Server.Transfer()只能用于在应用程序内重定向用户。
//This will work.
Response.Redirect("http://www.google.com");
//This will not work.
Server.Transfer("http://www.google.com");
其他回答
更多关于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()可用于将用户重定向到不属于应用程序的任何页面,但Server.Transfer()只能用于在应用程序内重定向用户。
//This will work.
Response.Redirect("http://www.google.com");
//This will not work.
Server.Transfer("http://www.google.com");
响应。重定向只是向浏览器发送一条消息(HTTP 302)。
服务器。传输在浏览器一无所知的情况下发生,浏览器请求一个页面,但服务器返回另一个页面的内容。
响应。重定向的代价更大,因为它向服务器添加了额外的行程,以确定要去哪里。
服务器。传输是更有效的,但它可能会误导用户,因为Url没有物理改变。
根据我的经验,性能上的差异还没有大到可以使用后一种方法
服务器。传输不会改变客户端浏览器中的URL,因此实际上浏览器并不知道您更改了另一个服务器端处理程序。响应。重定向告诉浏览器移动到另一个页面,因此标题栏中的url会发生变化。
服务器。传输稍微快一点,因为它避免了到服务器的一次往返,但不更改url可能对您有好处,也可能有坏处,这取决于您想要做什么。