asp.net在開發時,在不同的頁面間跳轉是我們常遇到的一件事,當一個複雜的邏輯在一個頁面放不下分成二個或多個頁面處理就需要在頁面間跳轉,用的最多還是使用者的登陸吧.
ASP.NET用的最多的跳轉是Response.Redirect,這個命令可以直接把請求重新導向到一個相對或絕對的路徑.它會把當前頁面的的Http流阻斷直接重新導向到新的URL.
而Server.Transfer,這個命令是由IIS伺服器直接在伺服器端執行跳轉,這個跳轉的路徑必須是相對路徑,也就是同一個網站下面的PATH或虛擬目眼錄.不能是外網的絕對路徑,因為直接是由當前請求頁面的同一個HTTP控制代碼指定請求同一網站上的路徑.
它們的區別在哪,先看下HTTP 頭吧,先看Response.Redirect
protected void Page_Load(object sender, EventArgs e)
{
Response.Redirect("/redirect.aspx"); //直接重新導向到本站的其路徑上
}
http://xml.test.com/HttpStatus.aspx -->這個是指我請求執行的頁面
GET /HttpStatus.aspx HTTP/1.1
Host: xml.test.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.0.14) Gecko/2009082707 Firefox/3.0.14
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-cn,zh;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: gb2312,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
HTTP/1.x 302 Found -->注意這個302
Cache-Control: private
Content-Type: text/html; charset=utf-8
Location: /redirect.aspx
Server: Microsoft-IIS/7.5
X-AspNet-Version: 2.0.50727
X-Powered-By: ASP.NET
Date: Sun, 01 Nov 2009 12:12:45 GMT
Content-Length: 133
----------------------------------------------------------
http://xml.test.com/redirect.aspx--> 這個是請求跳轉的頁面
GET /redirect.aspx HTTP/1.1
Host: xml.test.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.0.14) Gecko/2009082707 Firefox/3.0.14
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-cn,zh;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: gb2312,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
HTTP/1.x 200 OK
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Encoding: gzip
Vary: Accept-Encoding
Server: Microsoft-IIS/7.5
X-AspNet-Version: 2.0.50727
X-Powered-By: ASP.NET
Date: Sun, 01 Nov 2009 12:12:45 GMT
Content-Length: 489
而且地址欄的URL是定位到 http://xml.test.com/redirect.aspx
再來看看請求頁執行
public partial class HttpStatus : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Server.Transfer("/redirect.aspx");
}
}
得到的HTTP請求資訊是
http://xml.test.com/HttpStatus.aspx
GET /HttpStatus.aspx HTTP/1.1
Host: xml.test.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.0.14) Gecko/2009082707 Firefox/3.0.14
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-cn,zh;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: gb2312,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
HTTP/1.x 200 OK
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Encoding: gzip
Vary: Accept-Encoding
Server: Microsoft-IIS/7.5
X-AspNet-Version: 2.0.50727
X-Powered-By: ASP.NET
Date: Sun, 01 Nov 2009 12:17:21 GMT
Content-Length: 489
這二個不同的請求比較很明顯示了吧,HTTP REQUEST完全不一樣了
而且當前的URL還是停留在http://xml.test.com/HttpStatus.aspx而不是redirect.aspx這個路徑了.那麼這個命令其實只是在伺服器端執行了一個直接擷取redirect.aspx檔案然後回傳資料流給請求頁面,而Redirect是相當於使用者執行多一次請求,重新導向到了redirect.aspx.
Google在搜尋這塊也是有見議盡量不要用302跳轉,Redirect會產生一重新導向302狀態,表現層上區別不大,但對於搜尋引擎來說有些延時了.
看下GOOGLE的解釋
If you need to change the URL of a page as it is shown in search engine results, we recommended that you use a server-side 301 redirect. This is the best way to ensure that users and search engines are directed to the correct page. The 301 status code means that a page has permanently moved to a new location.
http://www.google.com/support/webmasters/bin/answer.py?hl=en&answer=93633
還有一個用的比較少的是Server.Execute,它的作用是把跳轉請求頁面的內容反回到當前的URL中來
HttpStatus.aspx
public partial class HttpStatus : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Server.Execute("/redirect.aspx");
}
}
redirect.aspx
public partial class redirect : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("Server.Execute");
}
}
當使用者直接請求HttpStatus.aspx時,頁面會顯示Server.Execute,它會直接把使用者請求頁面的值加在當前頁面中去,它的HTTP請求資訊如下
http://xml.test.com/HttpStatus.aspx
GET /HttpStatus.aspx HTTP/1.1
Host: xml.test.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.0.14) Gecko/2009082707 Firefox/3.0.14
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-cn,zh;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: gb2312,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Cache-Control: max-age=0
HTTP/1.x 200 OK
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Encoding: gzip
Vary: Accept-Encoding
Server: Microsoft-IIS/7.5
X-AspNet-Version: 2.0.50727
X-Powered-By: ASP.NET
Date: Sun, 01 Nov 2009 12:32:03 GMT
Content-Length: 552
GOOGLE見議我們用伺服器端跳轉,Server.Transfer就是伺服器端跳轉了,Redirect是用戶端請求跳轉,但什麼時候用Redirect什麼情況下用Server.Transfer呢?
總結下:
當需要把使用者跳轉到另一台伺服器上的頁面的時候 使用redirect
當需要把使用者跳轉到非aspx頁面時候,如html 使用redirect
需要把查詢字串作為url一部分的時候保留傳給伺服器的時候,因為其他2種方法不能做到2次postback,把資料先帶回伺服器 使用redirect
需要aspx頁面間的轉換(不涉及登入) 使用transfer
順便提一下,如何使用redirect方法在查詢字串中使用漢字,因為經常的情況是出現亂碼,原因是url不支援漢字。這個時候需要轉換:
string message =server.urlencode("梁遠華部落格");
在.NET上面控制項
一、伺服器控制項
HyperLink
通過NavigateUrl屬性來指定要跳轉的url。
主要用於由使用者來決定何時轉換頁面,但是用程式來控制轉換的目標。
二、程式控制
1、Response.Redirect()
伺服器--------》通知用戶端----------》請求跳轉到伺服器端
主要用於需要將查詢字串作為url的一部分傳遞給另一頁面,或跳轉的頁面是html頁面
2、Server.Transfer()
使用此方法最大的特點是url仍然是跳轉前的頁面的url。
主要用於需要將執行流程轉到同一web伺服器的另一aspx頁面時用。因為它在跳轉後保留了
request和session值,並且可以使用跳轉前頁面的資料。
還有一個值的一說的是
3、Server.Execute()
此方法執行完後程式控制權會返回到跳轉代碼的下一代碼,相當於我們的模態視窗,此時可以得到
跳轉過頁面的傳回值。
sever.execute 這個方法主要是用在頁面設計上面,而且他必須是跳轉同一網站下的頁面。這個方法是需要將一個頁面的輸出結果插入到另一個aspx頁面的時候使用,大部分是在表格中,將某一個頁面類似於嵌套的方式存在於另一頁面。
當需要把使用者跳轉到另一台伺服器上的頁面的時候 使用redirect
當需要把aspx頁面的輸出結果插入到另一個aspx頁面的時候使用 execute方法。
·如果要捕獲一個ASPX頁面的輸出結果,然後將結果插入另一個ASPX頁面的特定位置,則使用Server.Execute。
·如果要確保HTML輸出合法,請使用Response.Redirect,不要使用Server.Transfer或Server.Execute方法。
轉載地址:http://www.cnblogs.com/Leung/archive/2009/11/01/1594061.html