4

In an OnClick method for a button in an ASP.NET web form I have a call to Response.Redirect() which causes the system to abort the thread with the error message:

Exception thrown: 'System.Threading.ThreadAbortException' in mscorlib.dll

There's a few questions similar to this on here, using their solutions I changed:

Response.Redirect("~/UI/Home.aspx");

to

Response.Redirect("~/UI/Home.aspx", false);
Context.ApplicationInstance.CompleteRequest();

However I am still getting the same problem. Using the debugger I ran through the code and it all executed successfully until I called Response.Redirect();.

OnClick Function

protected void btnLogin_Click(object sender, EventArgs e)
    {
        SiteUser s = null;
        try
        {
            string email = txtEmail.Text;
            string pwd = txtPwd.Text;
            s = DBConnection.login(email, pwd);                
        }
        catch (Exception ex)
        {
            Console.Write(ex);
            lblLoginError.Text = "Error logging in.";
        }
        if (s != null)
        {
            Session["UserSession"] = s;
            Response.Redirect("~/UI/Home.aspx", false);
            Context.ApplicationInstance.CompleteRequest();
        }
        else
        {
            lblLoginError.Text = "User not found. Please check your details and try again.";
        }
    }

Any thoughts on why this might be happening?

Joe Schofield
  • 69
  • 1
  • 1
  • 7
  • 1
    `Response.Redirect()` *really shouldn't* be throwing that exception when given the `false` argument... – David Feb 20 '17 at 17:26
  • Possible duplicate of [Why Response.Redirect causes System.Threading.ThreadAbortException?](http://stackoverflow.com/questions/2777105/why-response-redirect-causes-system-threading-threadabortexception) – Am_I_Helpful Feb 20 '17 at 17:29
  • @Am_I_Helpful Very similar issue but it isn't resolved by the solutions there. – Joe Schofield Feb 21 '17 at 12:05

3 Answers3

6

I have seen this problem in the past. In theory, if you use this code, it shouldn't happen:

Response.Redirect(url, false);
Context.ApplicationInstance.CompleteRequest();

That being said, I still got these sometimes, which is really surprising. I am guessing that it sometimes occurs in the presence of an active finally block to signal the code to start cleaning itself up, although that doesn't seem to be the case for you.

The best fix I could come up with is to catch the error and ignore it.

protected void btnLogin_Click(object sender, EventArgs e)
{
    try
    {
        SiteUser s = null;
        try
        {
            string email = txtEmail.Text;
            string pwd = txtPwd.Text;
            s = DBConnection.login(email, pwd);                
        }
        catch (Exception ex)
        {
            Console.Write(ex);
            lblLoginError.Text = "Error logging in.";
        }
        if (s != null)
        {
            Session["UserSession"] = s;
            Response.Redirect("~/UI/Home.aspx", false);
            Context.ApplicationInstance.CompleteRequest();
        }
        else
        {
            lblLoginError.Text = "User not found. Please check your details and try again.";
        }
    }
    catch(System.Threading.ThreadAbortException)
    {
        //Do nothing.  The exception will get rethrown by the framework when this block terminates.
    }
}
John Wu
  • 50,556
  • 8
  • 44
  • 80
2

This turned out to be an issue that I had caused by redirecting back if the session didn't contain a specific element in the target page, and in this instance it didn't! The exception is still thrown but no longer causing visible problems.

Thanks

Joe Schofield
  • 69
  • 1
  • 1
  • 7
2

I Resolve this using

Response.Redirect(.....)
HttpContext.Current.Response.Flush(); // Sends all currently buffered output to the client.
HttpContext.Current.Response.SuppressContent = true;  // Gets or sets a value indicating whether to send HTTP content to the client.
HttpContext.Current.ApplicationInstance.CompleteRequest(); // Causes ASP.NET to bypass all events and filtering in the HTTP pipeline chain of execution and directly execute the EndRequest event.    

Reference: How to Avoid Response.End() "Thread was being aborted" Exception during the Excel file download

Pabli770
  • 131
  • 1
  • 8