When is it safe to do a Response.Redirect() without throwing an exception?
Posted
by DDechant
on Stack Overflow
See other posts from Stack Overflow
or by DDechant
Published on 2010-03-29T17:27:42Z
Indexed on
2010/03/29
17:53 UTC
Read the original article
Hit count: 225
I have an intermediary class extending System.Web.UI.Page for all of my pages that require authentication. The class mostly does custom authentication handling.
When a user with insufficient access attempts to visit a page, I try to redirect the user back to the login page while preventing any further page events from being executed (ie. Page_load). The first solution that came to mind was the default implementation of Response.Redirect. Of course the downside to this is the possibility of ThreadAbortExceptions being thrown.
So my question is this: When (if at all) during the page life cycle is it actually safe to execute Response.Redirect() without ThreadAbortException ever being thrown?
public class CustomPage : System.Web.UI.Page
{
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (!IsValid())
Response.Redirect("login.aspx", true);
}
}
© Stack Overflow or respective owner