Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
Previous Versions
.NET Framework 2.0
System.Web
HttpResponse Class
Redirect Method
 Redirect Method (String, Boolean)

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
.NET Framework Class Library
HttpResponse.Redirect Method (String, Boolean)

Redirects a client to a new URL. Specifies the new URL and whether execution of the current page should terminate.

Namespace: System.Web
Assembly: System.Web (in system.web.dll)

Visual Basic (Declaration)
Public Sub Redirect ( _
    url As String, _
    endResponse As Boolean _
)
Visual Basic (Usage)
Dim instance As HttpResponse
Dim url As String
Dim endResponse As Boolean

instance.Redirect(url, endResponse)
C#
public void Redirect (
    string url,
    bool endResponse
)
C++
public:
void Redirect (
    String^ url, 
    bool endResponse
)
J#
public void Redirect (
    String url, 
    boolean endResponse
)
JScript
public function Redirect (
    url : String, 
    endResponse : boolean
)

Parameters

url

The target location.

endResponse

Indicates whether execution of the current page should terminate.

Exception typeCondition

HttpException

A redirection is attempted after the HTTP headers have been sent.

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.

Redirect calls End which raises a ThreadAbortException exception upon completion.

The following code example uses the IsClientConnected property to check whether the client requesting the page remains connected to the server. If IsClientConnected is true, the code calls the Redirect method, and the client will view another page. If IsClientConnected is false, the code calls the End method and all page processing is terminated.

Visual Basic
<%@ Page Language="VB" %>
<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>
<head>
</head>
<body>
    <form runat="server">
    </form>
</body>
</html>
C#
<%@ Page Language="C#" %>
<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>
<head>
</head>
<body>
    <form runat="server">
    </form>
</body>
</html>

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
ThreadAbortException not raised when endResponse is false      David M. Kean   |   Edit   |   Show History
Response.End() is not called when endResponse is false. The page will fully process and the response header will be sent out.
Tags What's this?: Add a tag
Flag as ContentBug
Losing session information about a redirect      Harry Pfleger   |   Edit   |   Show History

If you do a Response.Redirect which results in Response.End() being called (either passing true as the 2nd paramter, or not passing a 2nd parameter at all), you will lose session data if it's the first time you write session information for the user. For example, after a login, you might set do:

Session["userId"] = userId;

Response.Redirect("admin.aspx");

The solution is to call Response.Redirect("admin.aspx", false);

 

You will only lose session data if the session does not yet exist when you do the redirect. What doesn't get set here is not the session data in itself, it's the cookie that identifies the session.

 

Betrand LeRoy explains this in greater detail:

http://weblogs.asp.net/bleroy/archive/2004/08/03/207486.aspx 

Tags What's this?: Add a tag
Flag as ContentBug
When Does redirect happen?      MikeHamilton   |   Edit   |   Show History
Does the redirect information get sent to the client when the response.redirect is executed or when the page finishes?
Tags What's this?: Add a tag
Flag as ContentBug
Even w/true, it doesn't _always_ end the response immediately!      WaldenL   |   Edit   |   Show History
Careful!! If you pass true, Response.Redirect() calls Response.End(), however there are certain circumstances where Response.End() does not raise the ThreadAbortException and instead sets a couple of flags and returns. One such situation is when you're in Global.asax, but it would occur more generically in any execution step that was not cancellable. I'll add details on Response.End(), but in a page you'd be fine, but in Global.asax processing will continue even if you pass true as the second parm.
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker