HttpResponse.Redirect 方法

定义

将客户端重定向到新的 URL。Redirects a client to a new URL.

重载

Redirect(String)

将请求重定向到新 URL 并指定该新 URL。Redirects a request to a new URL and specifies the new URL.

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)

将请求重定向到新 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

目标位置。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.

Redirect完成EndThreadAbortException引发异常的调用。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) ,而是使用重载并传递false endResponse参数,然后调用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)

将客户端重定向到新的 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

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

endResponse
Boolean

指示当前页的执行是否应终止。Indicates whether execution of the current page should terminate.

异常

urlnullurl is null.

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. 如果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.

适用于