服务器之间的区别是什么。转移和响应。重定向?
它们各自的优点和缺点是什么? 什么时候一种比另一种更合适? 什么时候不合适?
服务器之间的区别是什么。转移和响应。重定向?
它们各自的优点和缺点是什么? 什么时候一种比另一种更合适? 什么时候不合适?
当前回答
更多关于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
其他回答
响应。重定向的代价更大,因为它向服务器添加了额外的行程,以确定要去哪里。
服务器。传输是更有效的,但它可能会误导用户,因为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").
传输完全是服务器端的。客户端地址栏保持不变。请求之间上下文的传输有些复杂。刷新和重新启动页面处理程序可能是昂贵的,所以在管道的早期进行传输,例如在BeginRequest期间的HttpModule中。仔细阅读MSDN文档,并测试和理解HttpContext的新值。请求——特别是在回发场景中。我们通常使用服务器。转换为错误场景。
重定向以302状态终止请求,客户端往返响应并在内部吃掉一个异常(轻微的服务器性能打击-取决于你每天做多少)客户端然后导航到新地址。浏览器地址栏和历史更新等。客户端支付额外的往返费用-费用取决于延迟。在我们的业务中,我们重定向了很多,我们编写了自己的模块来避免异常成本。
服务器。传输不会改变客户端浏览器中的URL,因此实际上浏览器并不知道您更改了另一个服务器端处理程序。响应。重定向告诉浏览器移动到另一个页面,因此标题栏中的url会发生变化。
服务器。传输稍微快一点,因为它避免了到服务器的一次往返,但不更改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