语言

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)参数传递falseendResponse重载,然后调用该方法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>

注解

可以为目标位置指定绝对 URL(例如,例如, http://www.contoso.com/default.aspxDefault.aspx),但某些浏览器可能会拒绝相对 URL。

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

注释

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

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

适用于