Stepping on a rake
Whammm! I did it again! How many times do I have to tell myself that Response.Redirect(string) throws an ThreadAbortException?! Here is the short version:
private void Page_Load(object sender, System.EventArgs e)
{
try
{
Response.Redirect("page1.aspx");
}
catch
{
Response.Redirect("page2.aspx");
}
}
As expected(?), browser ends up on page2.aspx. So far so good but let’s do something smart and catch the exception (ignoring decoding/encoding and other mundane stuff):
private void Page_Load(object sender, System.EventArgs e)
{
try
{
Response.Redirect("page1.aspx");
}
catch (Exception ex)
{
Response.Redirect("page2.aspx?error=" + ex.ToString());
}
}
Browser ends up on page1.aspx?! Now that’s weird. Peeking in IL source with Reflector failed to reveal any substantial differences between the two. The answer, I guess, is buried somewhere inside HttpResponse.End and Thread.Abort methods which is more than my brain can digest. Explanation, anyone?
Note to myself: never place Redirect inside try{} block but if you have to, make sure it looks like this:
private void Page_Load(object sender, System.EventArgs e)
{
try
{
Response.Redirect("page1.aspx", false);
return;
}
catch (Exception ex)
{
Response.Redirect("page2.aspx?error=" + ex.ToString());
}
}