views:

31

answers:

1

I understand (now) that Response.Redirect() and Response.End() throw a ThreadAbortException as an expensive way of killing the current processing thread to emulate the behaviour of ASP Classic's Response.End() and Response.Redirect methods.

However.

It seems intermittently in our application that the exception bubbles too high. For example, we have a page that is called from client side javascript to return a small string to display in a page.

protected void Page_Load(object sender, EventArgs e)
{
      // Work out some stuff.
      Response.Write(stuff);
      Response.End();
}

This generally works, but sometimes, we get the exception bubbling up to the UI layer and get part of the exception text displayed in the page.

Similarly, else where we have:

// check the login is still valid:
if(!loggedin) {
  Response.Redirect("login.aspx");
}

In some cases, the user is redirected to login.aspx, in others, the user gets an ASP.NET error page and stack dump (because of how our dev servers are configured).

i.e. in some cases, response.redirect throws an exception all the way up INSTEAD of doing a redirect. Why? How do we stop this?

+1  A: 

Have you tried overloading the default Redirect method and not ending the response?

if(!loggedin) { 
     Response.Redirect("login.aspx", false); 
} 
Damien Dennehy
Worked out with some research what's the right thing to do, but, still want to understand why sometimes it bubbles an exception up and others it works.
THEMike
Why are you using Response.Write() to write text onto a page anyway? It's not the recommended method - it's more classic ASP than ASP.NET.
Damien Dennehy