PC Review


Reply
Thread Tools Rating: Thread Rating: 5 votes, 2.60 average.

"Thread was being aborted" exception on redirect - VERY weird!!

 
 
Alan Silver
Guest
Posts: n/a
 
      6th Dec 2005
Hello,

I have a page that is supposed to do some checking, and if OK, set a
session variable before redirecting to another page. The following code
is a simplified version, I have hard-coded the basket ID and removed a
load of extra checking...

public partial class GoToCheckout : Page {
public void Page_Load(Object o, EventArgs e) {
try {
Session["basketid"] = "178";
//x.Text = "redirecting to PreCheckout.aspx";
Response.Redirect("PreCheckout.aspx");
} catch (Exception ex) {
Response.Redirect("/?msg=" + ex.Message);
//x.Text = "exception - " + ex.Message;
}
}
}

When this page is called, it throws a "Thread was being aborted"
exception when it hits the line...

Response.Redirect("PreCheckout.aspx");

The catch block picks it up and redirects to the home page, putting the
exception message in the querystring. I did this because if I comment
out the redirect in the catch and instead use the (commented out) x.Text
bit (x is a literal, put on the otherwise empty .aspx file for
debugging), then the redirect works fine!!

If I comment out the redirect in the try block and uncomment the x.Text
line there, then the page displays the message "redirecting to
PreCheckout.aspx" exactly as expected.

So, it seems there's some weird problem with the redirect, but I can't
work it out. Nor can I work out why changing the behaviour of the code
in the catch block changes whether the exception is raised or not.

This code has been working fine for a few months now. I have recently
been changing pages to use master pages and themes, which is when I
noticed the problem. However, if I now change this page back to not use
a master or theme, the problem is still there. I am totally baffled, and
would appreciate any help you can offer. TIA

--
Alan Silver
(anything added below this line is nothing to do with me)
 
Reply With Quote
 
 
 
 
Joey
Guest
Posts: n/a
 
      6th Dec 2005
There is a second, optional parameter to the Response.Redirect call. It
is a boolean value that indicates whether or not to continue processing
the current page. Set it to false, and you should not get the errors
anymore.

Example:

Response.Redirect("PreCheckout.aspx", false);

JP

Alan Silver wrote:
> Hello,
>
> I have a page that is supposed to do some checking, and if OK, set a
> session variable before redirecting to another page. The following code
> is a simplified version, I have hard-coded the basket ID and removed a
> load of extra checking...
>
> public partial class GoToCheckout : Page {
> public void Page_Load(Object o, EventArgs e) {
> try {
> Session["basketid"] = "178";
> //x.Text = "redirecting to PreCheckout.aspx";
> Response.Redirect("PreCheckout.aspx");
> } catch (Exception ex) {
> Response.Redirect("/?msg=" + ex.Message);
> //x.Text = "exception - " + ex.Message;
> }
> }
> }
>
> When this page is called, it throws a "Thread was being aborted"
> exception when it hits the line...
>
> Response.Redirect("PreCheckout.aspx");
>
> The catch block picks it up and redirects to the home page, putting the
> exception message in the querystring. I did this because if I comment
> out the redirect in the catch and instead use the (commented out) x.Text
> bit (x is a literal, put on the otherwise empty .aspx file for
> debugging), then the redirect works fine!!
>
> If I comment out the redirect in the try block and uncomment the x.Text
> line there, then the page displays the message "redirecting to
> PreCheckout.aspx" exactly as expected.
>
> So, it seems there's some weird problem with the redirect, but I can't
> work it out. Nor can I work out why changing the behaviour of the code
> in the catch block changes whether the exception is raised or not.
>
> This code has been working fine for a few months now. I have recently
> been changing pages to use master pages and themes, which is when I
> noticed the problem. However, if I now change this page back to not use
> a master or theme, the problem is still there. I am totally baffled, and
> would appreciate any help you can offer. TIA
>
> --
> Alan Silver
> (anything added below this line is nothing to do with me)


 
Reply With Quote
 
 
 
 
Keith Patrick
Guest
Posts: n/a
 
      6th Dec 2005
I think what's happening is that your first Redirect is ending the page
execution, and thus the request/response. Try passing in "false" as a
second param to Redirect to see if that helps; it tells the redirect not to
end the thread on that call. Another option, since you're within your
application, is to use Server.Transfer instead, as it's more efficient
within the app.


 
Reply With Quote
 
Alan Silver
Guest
Posts: n/a
 
      6th Dec 2005
>There is a second, optional parameter to the Response.Redirect call. It
>is a boolean value that indicates whether or not to continue processing
>the current page. Set it to false, and you should not get the errors
>anymore.


Thanks for the reply. Shortly after posting (ain't that always the
way!!), I found http://tinyurl.com/cj3qp where someone points out that
using Response.Redirect inside a try/catch blocks throws this exception.
I moved it outside the block and the problem went away.

I may change it back and try it with this second parameter though as the
code was neater before.

Thanks for the reply.

--
Alan Silver
(anything added below this line is nothing to do with me)
 
Reply With Quote
 
Alan Silver
Guest
Posts: n/a
 
      6th Dec 2005
>I think what's happening is that your first Redirect is ending the page
>execution, and thus the request/response. Try passing in "false" as a
>second param to Redirect to see if that helps; it tells the redirect not to
>end the thread on that call. Another option, since you're within your
>application, is to use Server.Transfer instead, as it's more efficient
>within the app.


Thanks. See my reply to Joey ;-)

--
Alan Silver
(anything added below this line is nothing to do with me)
 
Reply With Quote
 
Alan Silver
Guest
Posts: n/a
 
      6th Dec 2005
>I may change it back and try it with this second parameter though as
>the code was neater before.


I did, and it was ;-)

Thanks again to both of you

--
Alan Silver
(anything added below this line is nothing to do with me)
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Exception of type 'System.Web.HttpUnhandledException' wasthrown.Exception has been thrown by the target of an invocation.System.WebSystem.Exception jobs Microsoft ASP .NET 1 16th Nov 2007 06:57 PM
Publish exception to Email in Exception Handling Exception Block =?Utf-8?B?ZW15?= Microsoft C# .NET 1 31st Mar 2005 12:45 PM
my pc very very very slow help me =?Utf-8?B?amFrZXRoZWd1bm5lcg==?= Windows XP General 3 18th Mar 2005 01:43 AM
XP Clock very very very fast =?Utf-8?B?T3JjYWRlcw==?= Windows XP Help 6 30th Dec 2004 08:01 PM
RIS - very very very very very slow =?Utf-8?B?TWF0dGhldyBFbGxpcw==?= Windows XP Setup 3 6th Dec 2004 11:58 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:46 PM.