XYZPDQ

RECENT

3/27/2009

Response.Redirect inside of a Try/Catch

Ok, first off, let me state, that I am not entirely certain WHY I did this in the first place.  I am going to say I was rushed and wasn’t thinking clearly.  However, I glazed over it and moved on and when it came time to test, I was getting bizarre behavior that didn’t throw an error.

Session[“var”] = “”; 

try 
{ 
    Session[“var”] = “Good Value”;

    Response.Redirect(“newpage.html”); 
} 
catch(Exception ex) 
{ 
    Session[“var”] = “Bad Value”; 
}

In the code above, Session[“var”] will ALWAYS equal “Bad Value”.  Why you may ask?

Response.Redirect throws a ThreadAbortException.  … Fun, no?

If you are building a URL inside of a try block that you want to then redirect to, declare a string, build your url, and then pass that on to the Response.Redirect statement. 

For Example:

string _url;

try 
{ 
    _url = “yourpage.aspx?var=” + iffyMethodCall(); 
} 
catch(InvalidOperationException ex) 
{ 
    _url = “error.html”; 
}

Response.Redirect(url);

So, If you ever run into odd behavior in a site you are working on and when debugging, your code goes straight past your Response.Redirect and into the catch block and the debugger starts giving you cryptic messages, this may be what you’re seeing.

Thursday, May 21, 2009 1:37:12 PM (GMT Daylight Time, UTC+01:00)
Sorry. I lived what most people call the good life. I was happy, but deep inside I always felt that, with the short amount of time we are given to live and love in this world, we spend too much time loving things instead of people.
I am from Mongolia and also now'm speaking English, please tell me right I wrote the following sentence: "External hard drive for free shipping asus pq turbo intel lga atx motherboard for after rebate free shipping."

With best wishes :o, Chabley.
Monday, July 20, 2009 7:13:51 PM (GMT Daylight Time, UTC+01:00)
Good evening. It's takin' whatever comes your way, the good AND the bad, that give life flavor. It's all the stuff rolled together that makes life worth livin'.
I am from Guinea and learning to speak English, please tell me right I wrote the following sentence: "Buy music stand light and led micro lights, order page size magnifiers bright."

Thanks for the help :D, Alexis.
Comments are closed.