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

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


当前回答

除了ScarletGarden的评论,你还需要考虑搜索引擎和你的重定向的影响。这个页面永久移动了吗?暂时的?这很重要。

看:响应。重定向vs。“301永久移动”:

We've all used Response.Redirect at one time or another. It's the quick and easy way to get visitors pointed in the right direction if they somehow end up in the wrong place. But did you know that Response.Redirect sends an HTTP response status code of "302 Found" when you might really want to send "301 Moved Permanently"? The distinction seems small, but in certain cases it can actually make a big difference. For example, if you use a "301 Moved Permanently" response code, most search engines will remove the outdated link from their index and replace it with the new one. If you use "302 Found", they'll continue returning to the old page...

其他回答

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

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

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

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

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

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

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

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

服务器。传输不会改变客户端浏览器中的URL,因此实际上浏览器并不知道您更改了另一个服务器端处理程序。响应。重定向告诉浏览器移动到另一个页面,因此标题栏中的url会发生变化。

服务器。传输稍微快一点,因为它避免了到服务器的一次往返,但不更改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.