服务器之间的区别是什么。转移和响应。重定向?
它们各自的优点和缺点是什么? 什么时候一种比另一种更合适? 什么时候不合适?
服务器之间的区别是什么。转移和响应。重定向?
它们各自的优点和缺点是什么? 什么时候一种比另一种更合适? 什么时候不合适?
当前回答
如上所述,有许多不同之处。除此之外,还有一个不同。Response.Redirect()可用于将用户重定向到不属于应用程序的任何页面,但Server.Transfer()只能用于在应用程序内重定向用户。
//This will work.
Response.Redirect("http://www.google.com");
//This will not work.
Server.Transfer("http://www.google.com");
其他回答
传输完全是服务器端的。客户端地址栏保持不变。请求之间上下文的传输有些复杂。刷新和重新启动页面处理程序可能是昂贵的,所以在管道的早期进行传输,例如在BeginRequest期间的HttpModule中。仔细阅读MSDN文档,并测试和理解HttpContext的新值。请求——特别是在回发场景中。我们通常使用服务器。转换为错误场景。
重定向以302状态终止请求,客户端往返响应并在内部吃掉一个异常(轻微的服务器性能打击-取决于你每天做多少)客户端然后导航到新地址。浏览器地址栏和历史更新等。客户端支付额外的往返费用-费用取决于延迟。在我们的业务中,我们重定向了很多,我们编写了自己的模块来避免异常成本。
响应。重定向的代价更大,因为它向服务器添加了额外的行程,以确定要去哪里。
服务器。传输是更有效的,但它可能会误导用户,因为Url没有物理改变。
根据我的经验,性能上的差异还没有大到可以使用后一种方法
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.
如上所述,有许多不同之处。除此之外,还有一个不同。Response.Redirect()可用于将用户重定向到不属于应用程序的任何页面,但Server.Transfer()只能用于在应用程序内重定向用户。
//This will work.
Response.Redirect("http://www.google.com");
//This will not work.
Server.Transfer("http://www.google.com");
响应。重定向在第一个页面到达客户端后将页面重定向到另一个页面。所以客户端知道重定向。
服务器。传输退出页面的当前执行。客户端不知道重定向。它允许您传输查询字符串和表单变量。
所以这取决于你的需要来选择哪个更好。