Click here to Skip to main content
13,453,774 members (45,119 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


9 bookmarked
Posted 13 Mar 2013

ASP.NET Response.Redirect without ThreadAbortException

, 13 Mar 2013
Rate this:
Please Sign up or sign in to vote.
Response.Redirect(string url) calls Response.End() which then throws a ThreadAbortException. Here's a simple way to solve it.


There are quite a few posts on the web about this issue. But I haven't found one that addresses all issues with Response.Redirect in an ASP.NET web form. We still need the method, and adding false to the endResponse flag is only step one of many steps.

If you aren't already aware of this issue.  Here's the story:

Response.End() exists for compatibility with original ASP. Its intent is to halt all execution for that page, preventing subsequent code from executing. Sounds like a great idea right? Problem is it may circumvent some managed code that you needed to execute. As well as causing an expensive ThreadAbortException to occur.

"Well I don't ever use that so I'm safe", you say? Do you use Response.Redirect? Then odds are you call it indirectly.

Response.Redirect(string url, bool endResponse)

So let's cut to the chase. Here's an extension that can be used pretty much anywhere and not require overriding the Page class.

public static void Redirect(this TemplateControl control, bool ignoreIfInvisible = true)
  Page page = control.Page;
  if (!ignoreIfInvisible || page.Visible)
    // Sets the page for redirect, but does not abort.
    page.Response.Redirect(url, false);
    // Causes ASP.NET to bypass all events and filtering in the HTTP pipeline
    // chain of execution and directly execute the EndRequest event.

    // By setting this to false, we flag that a redirect is set,
    // and to not render the page contents.
    page.Visible = false;

The important thing to note here is page.Visible = false. This prevents child controls from rendering as well as many data bound controls will not fire their events. This not only reduces the chance of an exception or redundant execution, it flags that a redirect is pending and therefore allows for easy control of code execution.  More importantly, this flag helps prevent unintended subsequent redirects.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
United States United States
Just a crazy developer with crazy ideas of grandure.

What's that? Anal probe? NO, I said "Anal CODE!" Get that thing away, and get back to EditPlus.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 4 Pin
Exception Duck10-May-14 5:37
memberException Duck10-May-14 5:37 
QuestionUrl is missing from your example. Pin
Exception Duck10-May-14 5:36
memberException Duck10-May-14 5:36 
GeneralMy vote of 5 Pin
Exception Duck10-May-14 5:35
memberException Duck10-May-14 5:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180318.3 | Last Updated 13 Mar 2013
Article Copyright 2013 by essence
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid