HttpResponse.Redirect 方法

定義

重新導向用戶端至新的 URL。

多載

Redirect(String)

重新導向要求至新的 URL 並指定新的 URL。

Redirect(String, Boolean)

重新導向用戶端至新的 URL。 指定新 URL 和是否應該結束目前網頁的執行。

Redirect(String)

重新導向要求至新的 URL 並指定新的 URL。

public:
 void Redirect(System::String ^ url);
public void Redirect (string url);
member this.Redirect : string -> unit
Public Sub Redirect (url As String)

參數

url
String

目標位置。 這可能是應用程式相對虛擬路徑。

例外狀況

在送出 HTTP 標頭之後嘗試重新導向。

範例

下列範例會強制無條件重新導向至另一個網站。

Response.Redirect("http://www.microsoft.com/gohere/look.htm");

Response.Redirect("http://www.microsoft.com/gohere/look.htm")
   

備註

呼叫 Redirect 相當於呼叫 Redirect ,並將第二個參數設定為 true

Redirect會在完成時擲回例外狀況的ThreadAbortException呼叫End。 此例外狀況對 Web 應用程式效能造成負面影響。 因此,建議您不要使用此多載,而是針對 endResponse 參數使用 HttpResponse.Redirect(String, Boolean) 多載並傳遞 false ,然後呼叫 CompleteRequest 方法。 如需詳細資訊,請參閱 End 方法。

注意

僅限行動頁面,如果您的應用程式依賴無 Cookie 會話,或可能從需要無 Cookie 會話的行動裝置接收要求,使用波浪圖 (~ 路徑中的) 可能會導致建立新的會話,並可能遺失會話數據。 若要使用 “~/path” 之類的路徑在行動控件上設定屬性,請在將路徑指派給屬性之前,先使用 ResolveUrl “~/path” 解析路徑。

ASP.NET 傳回 302 HTTP 狀態代碼來執行重新導向。 將控制權傳送到另一個頁面的替代方式是 Transfer 方法。 方法 Transfer 通常更有效率,因為它不會對客戶端進行往返。 如需詳細資訊,請參閱 如何:將使用者重新導向至另一個頁面

適用於

Redirect(String, Boolean)

重新導向用戶端至新的 URL。 指定新 URL 和是否應該結束目前網頁的執行。

public:
 void Redirect(System::String ^ url, bool endResponse);
public void Redirect (string url, bool endResponse);
member this.Redirect : string * bool -> unit
Public Sub Redirect (url As String, endResponse As Boolean)

參數

url
String

目標的位置。

endResponse
Boolean

表示是否應該終止目前網頁的執行。

例外狀況

urlnull

url 包含新行字元。

在送出 HTTP 標頭之後嘗試重新導向。

頁面要求是回呼的結果。

範例

下列範例會 IsClientConnected 使用 屬性來檢查要求頁面的用戶端是否仍連線到伺服器。 如果 IsClientConnected 為 true,程式代碼會呼叫 Redirect 方法,而用戶端將會檢視另一個頁面。 如果 IsClientConnected 為 false,則程式代碼會呼叫 方法, End 並終止所有頁面處理。

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

    private void Page_Load(object sender, EventArgs e)
    {
        // Check whether the browser remains
        // connected to the server.
        if (Response.IsClientConnected)
        {
            // If still connected, redirect
            // to another page. 
            Response.Redirect("Page2CS.aspx", false);
        }
        else
        {
            // If the browser is not connected
            // stop all response processing.
            Response.End();
        }
    }

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">
    </form>
</body>
</html>
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    Private Sub Page_Load(sender As Object, e As EventArgs)

        ' Check whether the browser remains
        ' connected to the server.
        If (Response.IsClientConnected) Then

            ' If still connected, redirect
            ' to another page.             
            Response.Redirect("Page2VB.aspx", false)
        Else
            ' If the browser is not connected
            ' stop all response processing.
            Response.End()
        End If
    End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">
    </form>
</body>
</html>

備註

例如, http://www.contoso.com/default.aspx) 或相對 URL (等絕對 URL (,可以針對目標位置指定Default.aspx) ,但某些瀏覽器可能會拒絕相對 URL。

當您在頁面處理程式中使用此方法來終止一頁的要求,並啟動另一個頁面的新要求時,請將 設定 endResponsefalse ,然後呼叫 CompleteRequest 方法。 如果您為 參數指定 true ,這個方法會End呼叫原始要求的 方法,這會在完成時擲回ThreadAbortException例外endResponse狀況。 此例外狀況對 Web 應用程式效能造成負面影響,這就是為什麼建議針對 參數傳遞falseendResponse的原因。 如需詳細資訊,請參閱 End 方法。

注意

針對行動頁面,如果您的應用程式依賴無 Cookie 會話,或可能從需要無 Cookie 會話的行動裝置接收要求,在路徑中使用波浪 (~) 可能會建立新的會話,並可能會遺失會話數據。 若要使用 “~/path” 之類的路徑在行動控件上設定屬性,請在將路徑指派給屬性之前,先使用 ResolveUrl “~/path” 解析路徑。

ASP.NET 傳回 302 HTTP 狀態代碼來執行重新導向。 將控制權傳送到另一個頁面的替代方式是 Transfer 方法。 方法 Transfer 通常更有效率,因為它不會對客戶端進行往返。 如需詳細資訊,請參閱 如何:將使用者重新導向至另一個頁面

適用於