Skip to main content

Command Palette

Search for a command to run...

Response.Redirect: true or false?

Updated
2 min read
Response.Redirect: true or false?
F

Performance freak

When you use Response.Redirect with its second parameter set to true, it triggers a call to Response.End()

Response.Redirect("Login.aspx");              // default is true
Response.Redirect("Login.aspx", true);        // same as above

This causes ASP.NET to throw a ThreadAbortException, which is not ideal from a performance or diagnostics standpoint.

So, What Should We Do?

The recommended approach is to use false it as the second parameter. This allows the redirection to occur without forcibly terminating the current request:

Response.Redirect("Login.aspx", false);
HttpContext.Current.ApplicationInstance.CompleteRequest();
return;

Calling CompleteRequest() ensures that the request finishes gracefully — a "soft" redirect, if you will.

But There's a Catch in Web Forms

When using Response.Redirect(..., false) in ASP.NET Web Forms, you may encounter issues with certain page-level components like LinqDataSource.

These components continue executing their logic even after the redirect is issued. If a required parameter (e.g., UserId) isn’t provided in time, you'll get a type mismatch error — typically because these controls bind data early in the page lifecycle.

Common error:

[ParseException: Operator '==' incompatible with operand types 'Guid' and 'Object']

To avoid this error, if you're setting WhereParameters in Page_LoadYou must use Response.Redirect(..., true) to immediately terminate the pipeline before any control processing begins.

Want to Use false and Still Avoid Errors?

If you’d like to avoid ThreadAbortException and safely use controls like LinqDataSource, the best practice is to perform your redirect check inside the Page_Init method.

Why? Because Page_Init occurs very early in the page lifecycle — before data binding or parameter evaluation. So redirecting here will safely exit the page before LinqDataSource or similar controls need their parameters.

Here’s how it looks:

protected void Page_Init(object sender, EventArgs e)
{
    if (Request.Cookies["isAuthenticated"] == null)
    {
        Response.Redirect("Login.aspx", false);
        HttpContext.Current.ApplicationInstance.CompleteRequest();
        return;
    }
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        LinqDataSource1.WhereParameters["UserId"].DefaultValue = GetUserId().ToString();
    }
}

Final Thoughts

Using false with CompleteRequest() is a cleaner, more graceful way to redirect in ASP.NET Web Forms — but only if you're careful with the page lifecycle.
Move your redirect checks to Page_Init whenever you’re working with components that bind data early, you’ll avoid exceptions and headaches.