HttpResponse.Redirect 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将客户端重定向到新的 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.aspx) 或相对 URL (,可以为目标位置指定Default.aspx) ,但某些浏览器可能会拒绝相对 URL。
在页面处理程序中使用此方法来终止对一页的请求并启动另一页的新请求时,请将 false 设置为 endResponse ,然后调用 CompleteRequest 方法。 如果为 参数指定 true ,此方法将调用End原始请求的 方法,这会在请求完成时引发ThreadAbortExceptionendResponse异常。 此异常对 Web 应用程序性能有不利影响,这就是建议为 参数传递falseendResponse的原因。 有关更多信息,请参见 End 方法。
注意
对于移动页面,如果应用程序依赖于无 Cookie 会话,或者可能从需要无 Cookie 会话的移动设备接收请求,则使用路径中的波形符 (~) 可能会创建新会话,并可能丢失会话数据。 若要使用路径(如“~/path”)在移动控件上设置属性,请在将路径分配给属性之前使用 ResolveUrl “~/path”解析该路径。
ASP.NET 通过返回 302 HTTP 状态代码来执行重定向。 将控制权转移到另一页的另一种方法是 Transfer 方法。 方法 Transfer 通常更有效,因为它不会导致客户端往返。 有关详细信息,请参阅 如何:将用户重定向到其他页面。