Export (0) Print
Expand All

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)

public void Redirect (
	string url,
	bool endResponse
)
public void Redirect (
	String url, 
	boolean endResponse
)
public function Redirect (
	url : String, 
	endResponse : boolean
)
Not applicable.

Parameters

url

The target location.

endResponse

Indicates whether execution of the current page should terminate.

Exception typeCondition

ArgumentNullException

url is a null reference (Nothing in Visual Basic).

ArgumentException

url contains a newline character.

HttpException

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

ApplicationException

The page request is the result of a callback.

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.

NoteNote:

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 inadvertently creating a new session and potentially losing session data. 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.

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.

<%@ 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>

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

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft