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 调用 End 在完成时引发 ThreadAbortException 异常。 此异常对 Web 应用程序性能有不利影响。 因此,我们建议不要使用此重载,而是对参数使用HttpResponse.Redirect(String, Boolean)endResponse重载并传递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

指示当前页的执行是否应终止。

例外

url 上声明的默认值为 null

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。

在页面处理程序中使用此方法终止一个页面的请求并启动另一页的新请求时,请设置为endResponse``false该方法并调用该方法CompleteRequest。 如果为参数指定true,此方法将调用End原始请求的方法,这会在完成时引发ThreadAbortExceptionendResponse异常。 此异常对 Web 应用程序性能有不利影响,这就是为什么建议为endResponse参数传递false的原因。 有关更多信息,请参见 End 方法。

备注

对于移动页面,如果应用程序依赖于无 Cookie 会话,或者可能从需要无 Cookie 会话的移动设备接收请求,则使用路径中的平铺 (~) 可能会创建新会话并可能丢失会话数据。 若要使用路径(如“/path”~)在移动控件上设置属性,请在将路径分配给属性之前使用 ResolveUrl “/path”~解析路径。

ASP.NET 通过返回 302 HTTP 状态代码来执行重定向。 将控件传输到另一页的另一种方法是 Transfer 该方法。 该方法 Transfer 通常更高效,因为它不会导致往返客户端。 有关详细信息,请参阅 如何:将用户重定向到另一页

适用于