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

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


当前回答

除了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...

其他回答

响应。重定向涉及一个额外的往返和更新地址栏。

服务器。传输不会导致地址栏更改,服务器会用来自另一个页面的内容响应请求

e.g.

响应。重定向:

在客户机上,浏览器请求一个页面http://InitiallyRequestedPage.aspx 在服务器上响应请求,302传递重定向地址http://AnotherPage.aspx。 在客户机上,浏览器向地址http://AnotherPage.aspx发出第二个请求。 在服务器上响应来自http://AnotherPage.aspx的内容

服务器。转让:

在客户机上,浏览器请求一个页面http://InitiallyRequestedPage.aspx 在服务器上。转至http://AnotherPage.aspx 在服务器上,对http://InitiallyRequestedPage.aspx的请求做出响应,从http://AnotherPage.aspx返回内容

响应。重定向

优点:- RESTful -它改变地址栏,地址可以用来记录请求之间的状态变化。

缺点:- 慢——在客户端和服务器之间有一个额外的往返。当客户端和服务器之间存在大量延迟时,这样做的代价可能会很高。

服务器。转移

优点:- 快。

缺点:- 状态丢失-如果您正在使用服务器。转移来改变应用程序的状态以响应回贴,如果页面随后被重新加载,该状态将丢失,因为地址栏将与第一次请求时相同。

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

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

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

响应。重定向:告诉浏览器可以在新的位置找到所请求的页面。然后,浏览器向新页面发起另一个请求,在浏览器中加载其内容。这将导致浏览器产生两个请求。

服务器。转移:它将执行从服务器上的第一个页面转移到第二个页面。就浏览器客户端而言,它只发出一个请求,初始页面就是响应内容的页面。 这种方法的好处是减少了一次从客户机浏览器到服务器的往返。此外,任何提交的表单变量和查询字符串参数也可用于第二个页面。

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

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

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