0

Response.Redirect throws an exception?!!

by volkanuzun 11. August 2008 16:56

One of my collegue asked my help today about his login page throwing an exception. He is using membership provider to authenicate the users however he is not using role provider. In the login page code behind file, he authenticates the user, and checks the user's role and Response.Redirect to a new  page depending on the role. (Dont ask me why not role provider?). A sample code is :

   try{
        if(User.IsInRole("guest")
             Response.Redirect("/guest.aspx");
       else if(User.IsInRole("member")
             ....
      }
     catch{
     .....
}

Now that the above code is always throwing an exception, i googled it, and found out that Response.Redirect(url,bool end) , the second parameter means end the current thread if it is true. By default it is true, which means kill the thread and redirect to url, however, the code is in a try catch block, which wont simply kill the thread, and will throw an exception. The easiest solution will be changing true to false; however i am not suer about the performance when you let the current thread run to finish and navigate to the second page. So i recommended him removing try catch :)

Tags:

Powered by BlogEngine.NET 1.6.0.0
Original Design by Laptop Geek, Adapted by onesoft