HttpResponse.Redirect HttpResponse.Redirect HttpResponse.Redirect HttpResponse.Redirect Method

定義

重新導向用戶端至新的 URL。Redirects a client to a new URL.

多載

Redirect(String) Redirect(String) Redirect(String) Redirect(String)

重新導向要求至新的 URL 並指定新的 URL。Redirects a request to a new URL and specifies the new URL.

Redirect(String, Boolean) Redirect(String, Boolean) Redirect(String, Boolean) Redirect(String, Boolean)

重新導向用戶端至新的 URL。Redirects a client to a new URL. 指定新 URL 和是否應該結束目前網頁的執行。Specifies the new URL and whether execution of the current page should terminate.

Redirect(String) Redirect(String) Redirect(String) Redirect(String)

重新導向要求至新的 URL 並指定新的 URL。Redirects a request to a new URL and specifies the new 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 String String String

目標位置。The target location. 這可能是應用程式相對虛擬路徑。This may be an application-relative virtual path.

例外狀況

在送出 HTTP 標頭之後嘗試重新導向。A redirection is attempted after the HTTP headers have been sent.

範例

下列範例會強制無條件重新導向到另一個網站。The following example forces an unconditional redirection to another Web site.

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

備註

呼叫Redirect相當於呼叫Redirect第二個參數設定為trueCalling Redirect is equivalent to calling Redirect with the second parameter set to true.

Redirect 呼叫End哪一個會擲回ThreadAbortException完成時的例外狀況。Redirect calls End which throws a ThreadAbortException exception upon completion. 這個例外狀況有不利的影響,對 Web 應用程式效能。This exception has a detrimental effect on Web application performance. 因此,我們建議您使用而不是這個多載HttpResponse.Redirect(String, Boolean)多載,並傳遞falseforendResponse參數,然後再呼叫CompleteRequest方法。Therefore, we recommend that instead of this overload you use the HttpResponse.Redirect(String, Boolean) overload and pass false for the endResponse parameter, and then call the CompleteRequest method. 如需詳細資訊,請參閱 End 方法。For more information, see the End method.

注意

行動頁面,如果您的應用程式依賴無 cookie 工作階段,或可能會接收來自行動裝置需要無 cookie 工作階段的要求,使用波狀符號 (~) 的路徑可能會導致建立新的工作階段,可能會遺失工作階段資料。For mobile pages only, if your application relies on cookieless sessions, or might receive requests from mobile devices that require cookieless sessions, using a tilde (~) in a path can result in creating a new session and potentially losing session data. 行動控制項的路徑上設定屬性,例如"~/path",解析路徑使用ResolveUrl"~/path 」 再將它指派給屬性。To set a property on a mobile control with a path such as "~/path", resolve the path using ResolveUrl "~/path" before assigning it to the property.

ASP.NET 會藉由傳回 302 HTTP 狀態碼來執行重新導向。ASP.NET performs the redirection by returning a 302 HTTP status code. 若要將控制權轉移到另一頁的替代方式是Transfer方法。An alternative way to transfer control to another page is the Transfer method. Transfer方法是通常更有效率,因為它不會往返造成用戶端。The Transfer method is typically more efficient because it does not cause a round trip to the client. 如需詳細資訊,請參閱如何:將使用者重新導向至其他頁面For more information, see How to: Redirect Users to Another Page.

Redirect(String, Boolean) Redirect(String, Boolean) Redirect(String, Boolean) Redirect(String, Boolean)

重新導向用戶端至新的 URL。Redirects a client to a new URL. 指定新 URL 和是否應該結束目前網頁的執行。Specifies the new URL and whether execution of the current page should terminate.

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 String String String

目標的位置。The location of the target.

endResponse
Boolean Boolean Boolean Boolean

表示是否應該終止目前網頁的執行。Indicates whether execution of the current page should terminate.

例外狀況

url 包含新行字元。url contains a newline character.

在送出 HTTP 標頭之後嘗試重新導向。A redirection is attempted after the HTTP headers have been sent.

頁面要求是回呼的結果。The page request is the result of a callback.

範例

下列範例會使用IsClientConnected屬性來檢查用戶端所要求的頁面是否仍然連接到伺服器。The following example uses the IsClientConnected property to check whether the client that is requesting the page remains connected to the server. 如果IsClientConnected為 true,程式碼會呼叫Redirect方法和用戶端將檢視另一個頁面。If IsClientConnected is true, the code calls the Redirect method, and the client will view another page. 如果IsClientConnected為 false,則程式碼會呼叫End方法,而所有的頁面處理終止。If IsClientConnected is false, then the code calls the End method and all page processing is terminated.

<%@ 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>

備註

絕對 URL (例如http://www.contoso.com/default.aspx)或相對的 URL (例如 Default.aspx) 可以指定目標位置,但某些瀏覽器可能會拒絕的相對 URL。An absolute URL (for example, http://www.contoso.com/default.aspx) or a relative URL (for example, Default.aspx) can be specified for the target location but some browsers may reject a relative URL.

當您使用這個方法的頁面處理常式中結束要求,一頁,並開始新的要求,另一個頁面,設定endResponsefalse,然後呼叫CompleteRequest方法。When you use this method in a page handler to terminate a request for one page and start a new request for another page, set endResponse to false and then call the CompleteRequest method. 如果您指定trueforendResponse參數,這個方法會呼叫End原始要求,則會擲回方法ThreadAbortException完成時的例外狀況。If you specify true for the endResponse parameter, this method calls the End method for the original request, which throws a ThreadAbortException exception when it completes. 這個例外狀況有不利的影響,在 Web 應用程式效能,這就是為什麼傳遞false針對endResponse建議參數。This exception has a detrimental effect on Web application performance, which is why passing false for the endResponse parameter is recommended. 如需詳細資訊,請參閱 End 方法。For more information, see the End method.

注意

行動裝置頁面中,如果您的應用程式依賴無 cookie 工作階段,或可能會接收來自行動裝置需要無 cookie 工作階段的要求,使用波狀符號 (~) 之路徑中可以建立新的工作階段並可能會遺失工作階段資料。For mobile pages, if your application relies on cookieless sessions, or might receive requests from mobile devices that require cookieless sessions, using a tilde (~) in a path can create a new session and potentially lose session data. 行動控制項的路徑上設定屬性,例如"~/path",解析路徑使用ResolveUrl"~/path 」 再將它指派給屬性。To set a property on a mobile control with a path such as "~/path", resolve the path using ResolveUrl "~/path" before assigning it to the property.

ASP.NET 會藉由傳回 302 HTTP 狀態碼來執行重新導向。ASP.NET performs the redirection by returning a 302 HTTP status code. 若要將控制權轉移到另一頁的替代方式是Transfer方法。An alternative way to transfer control to another page is the Transfer method. Transfer方法是通常更有效率,因為它不會往返造成用戶端。The Transfer method is typically more efficient because it does not cause a round trip to the client. 如需詳細資訊,請參閱如何:將使用者重新導向至其他頁面For more information, see How to: Redirect Users to Another Page.

適用於