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 ,並將第二個參數true設定為。Calling Redirect is equivalent to calling Redirect with the second parameter set to true.

RedirectEnd 完成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)是針對endResponse參數false使用多載和 pass,然後呼叫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 會話之行動裝置的要求~,在路徑中使用波狀符號()可能會導致建立新的會話,而且可能會遺失會話data.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>

備註

您可以為目標位置指定絕對http://www.contoso.com/default.aspxurl (例如)或相對 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. 如果您true ThreadAbortException為參數指定,這個方法會呼叫原始End要求的方法,它會在完成時擲回例外狀況。 endResponseIf 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.

適用於