Response.Redirect: true or false?

Performance freak
Search for a command to run...

Performance freak
No comments yet. Be the first to comment.
Using ThreadPool.QueueUserWorkItem: Helps bypass limitations when trying to run async code inside IHttpModule. Does not interfere with the ASP.NET pipeline. Email sending does not block the main thread. Calling SendMailAsync().Wait() is safe here...

Introduction In SQL Server, the bit data type is used to store boolean values (0 for false, 1 for true). However, when working with SQL Server in a .NET application using Entity Framework or LINQ to SQL, the bit field is automatically converted to a ...

System.Threading.ThreadAbortException: Thread was being aborted. at System.Threading.Thread.AbortInternal() To avoid the ThreadAbortException error when using Response.Redirect, setting the second parameter to false is generally sufficient. However,...

In Visual Studio; Solution Explorer-Project-Properties-Application. Check "Auto-Generate binding redirects" When this is selected, versions are specified in the web.config file after the Nuget package updates. When is binding necessary? If there is...

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.
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.
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.
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();
}
}
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.