Context: See original thread
http://www.asp.net/Forums/ShowPost.aspx?tabindex=1&PostID=294673 Question:
I simplified original code to the bare minimum and still below Code 1 did not work as expected. I used .NET Framework 1.0.3705. Can you some of you conduct the test by just creating one aspx with either Code 1 or Code 2 and tell us what's happening here.
1. Code 1: *DID NOT* get to the page google. It get to http://www.asp.net.
Hi, This one's tricky. To understand what happens, you have to know how redirect works. Basically, it adds a redirection header to the response, and it calls Response.End. This is where you get all the trouble from: End is really aborting the thread, which
is where you get a ThreadAbortException. It's probably this exception you're catching when redirecting. And of course, the second redirect wins... Solution? Use the non-aborting version of Redirect: Response.Redirect("http://www.asp.net", false); Hope this
helps ans solves your issue.
Bertrand
----
This posting is provided "AS IS" with no warranties, and confers no rights.
Bertrand, 1. ::Response.Redirect("http://www.asp.net", false); Still not working :-D 2. ::To understand what happens, you have to know how redirect works So, Catch me if you can? says 'try/catch/response.redirect.' :-D
Where there is a will, there is a way.
and where there is a team, there is more than one way.
Bertrand, 1. It worked! The game is over :-D . Still, who would know that *ASP.NET trick* ? :-D :-D Now, do you consider writing a book for ASP.NET trick & tips? :-D 2. Working code:
3. Just read MSDN: 1. Redirects a client to a new URL and specifies the new URL. [C#] public void Redirect(string); 2. Redirects a client to a new URL. Specifies the new URL and whether execution of the current page should terminate. [C#] public void
Redirect(string, bool);
Where there is a will, there is a way.
and where there is a team, there is more than one way.
Hi belroy Phouc, If what you are saying is true, then the following should throw an exeption and the catch should execute, and the output should be... -- Catch as Catch Can! --. But it doesn't, it redirects in the try. Also if the Response.Redirect in the try
calls end, for the moment I'm going to interpret that as a Response.End, shouldn't that end the program and redirect from the try? Any way, the following redirects to google. Am I the only on confused here?
JB, bleroy,JB: 1. You're not at all confused. Your question is exactly like my
Code 2 of my original post. You both can see it again. Excellent question. You should be part of ASP.NET team too :-D
bleroy, ::This one's tricky. To understand what happens, you have to know how redirect works. Basically, it adds a redirection header to the response, and it calls Response.End. This is where you get all the trouble from: End is really aborting the thread,
which is where you get a ThreadAbortException. It's probably this exception you're catching when redirecting. And of course, the second redirect wins... Your explaination *does not* to hold for all cases the way you explained. Is there something missing here?
Catch me if you can? Part II. :-D
Where there is a will, there is a way.
and where there is a team, there is more than one way.
JB, bleroy, 0. In addition to previous post Posted: 30 Jul 2003 11:03 PM Posted: 30 Jul 2003 11:21 PM 1. ::This one's tricky. To understand what happens, you have to know how redirect works. Basically, it adds a redirection header to the response, and
it calls Response.End. This is where you get all the trouble from: End is really aborting the thread, which is where you get a ThreadAbortException. It's probably this exception you're catching when redirecting. And of course, the second redirect wins... Still
this is NOT true for another Code 3 of point 2. I have Server.Transfer instead of Response.Redirect. If bleroy's statement is true, we should get www.asp.net for Code 3 but instead we get to google as expected. 2.
Code 3
3. This is just a 'pure hypothetical' question only:
It looks like .NET compiler (C# OR VB.net) *does not* translate well the source code into MSIL
for the Code 1 where we have a Redirect within the catch:
Is there a way, we can see the MSIL code generated by the .NET compiler for all Code 1,2,3 and JB's case to see. Chances are we may (and only may) have an error at the translation level by the compiler and the error is not caused by ASP.NET.
This is just a question only. 4. Other possible causes or explainations are to be seen. Any one else for Catch me if you can? :-D
Where there is a will, there is a way.
and where there is a team, there is more than one way.
JB, bleroy, 1. Response.Redirect- HTTP Location header HTTP Redirect response uses an HTTP status code of either "301 Moved Permanently" or "302 Move Temporarily" accompanied by a Location header containing a valid or new URL. If the client receives
an HTTP response with status 301 or 302, it will immediately request the resource at the URL in the Location header. If no Location header exists, the browser will display the status code message (indicating that the item has moved) . The browser should respond
identically to either HTTP status code 301 or 302 with one exception; in response to a "301 Moved Permanently" status code, the browser should update any bookmarks pointing to the old location.
2. With point 1, I conduct another test about the 'Response.Redirect' using another environment Tomcat Server 4.x/Jdk1.4.x and JSP Code 4 of point 3. It worked as expected. So environmentwise, it's different but that's not the key point, the key point
is both ASP.NET server and Tomcat server or any Java-based Servers (BEA Logic, IBM Websphere) have the same HTTP specification about Response.Redirect, the underlying implementation code is different which is OK, however functionalitywise, it should be the
same for all web servers for that Response.Redirect- HTTP Location header. So why it worked for Tomcat server and not in ASP.NET environment? Is Tomcat server better than APS.NET server? :-D
3. Code 4: JSP and Tomcat Server 4.x/Jdk1.4.x
Where there is a will, there is a way.
and where there is a team, there is more than one way.
Hi Phuoc, Very interesting how it works on a Tomcat Server. I guess I should have pointed out that I am on Win 2K Server using .Net v1.1.4322. Although, bleroy offered a workaround that does indeed work, I'm more curious at this point as to why this is happening.
I guess I'm just a control freak, but I like to know why things happen the way the do. If I was dealing with the universe or modern physics, I would be content to chalk it up to things that can't be explained at the moment, however in computer world there
must be some explanation. So my question remains.... Is this what's supposed to happen or did I stumble upon an oversight of some kind? :-D Curiously, JB
John Belthoff
Dodge, Duck, Dip, Dive & Dodge
If a man can dodge a wrench, he can dodge a ball!
Phuoc
Participant
1150 Points
230 Posts
Catch me if you can? by try/catch/response.redirect. Need expert to explain.
Jul 30, 2003 11:06 PM|LINK
private void Page_Load(object sender, System.EventArgs e) { try { Response.Redirect("http://www.google.com"); } catch (Exception ex) { Response.Redirect("http://www.asp.net"); } }2. Code 2: get to the page google if we commented out //Response.Redirect("http://www.asp.net");private void Page_Load(object sender, System.EventArgs e) { try { Response.Redirect("http://www.google.com"); } catch (Exception ex) { //Response.Redirect("http://www.asp.net"); } }and where there is a team, there is more than one way.
bleroy
All-Star
15617 Points
2302 Posts
Re: Catch me if you can? by try/catch/response.redirect. Need expert to explain.
Jul 30, 2003 11:22 PM|LINK
----
This posting is provided "AS IS" with no warranties, and confers no rights.
Phuoc
Participant
1150 Points
230 Posts
Re: Catch me if you can? by try/catch/response.redirect. Need expert to explain.
Jul 30, 2003 11:28 PM|LINK
and where there is a team, there is more than one way.
bleroy
All-Star
15617 Points
2302 Posts
Re: Catch me if you can? by try/catch/response.redirect. Need expert to explain.
Jul 30, 2003 11:47 PM|LINK
----
This posting is provided "AS IS" with no warranties, and confers no rights.
Phuoc
Participant
1150 Points
230 Posts
Re: Catch me if you can? by try/catch/response.redirect. Need expert to explain.
Jul 30, 2003 11:54 PM|LINK
private void Page_Load(object sender, System.EventArgs e) { try { Response.Redirect("http://www.google.com",false); } catch (Exception ex) { Response.Redirect("http://www.asp.net",false); } }3. Just read MSDN: 1. Redirects a client to a new URL and specifies the new URL. [C#] public void Redirect(string); 2. Redirects a client to a new URL. Specifies the new URL and whether execution of the current page should terminate. [C#] public void Redirect(string, bool);and where there is a team, there is more than one way.
JBelthoff
Member
553 Points
135 Posts
Re: Catch me if you can? by try/catch/response.redirect. Need expert to explain.
Jul 31, 2003 03:03 AM|LINK
private void Page_Load(object sender, System.EventArgs e) { try { Response.Redirect("http://www.google.com"); } catch (Exception ex) { Response.Write("Catch as Catch Can!"); } }:) JBDodge, Duck, Dip, Dive & Dodge
If a man can dodge a wrench, he can dodge a ball!
Phuoc
Participant
1150 Points
230 Posts
Re: Catch me if you can? by try/catch/response.redirect. Need expert to explain.
Jul 31, 2003 03:21 AM|LINK
and where there is a team, there is more than one way.
Phuoc
Participant
1150 Points
230 Posts
Re: Catch me if you can? by try/catch/response.redirect. Need expert to explain.
Jul 31, 2003 06:45 AM|LINK
private void Page_Load(object sender, System.EventArgs e) { try { Response.Redirect("http://www.google.com"); } catch (Exception ex) { Server.Transfer("http://www.asp.net"); } }3. This is just a 'pure hypothetical' question only: It looks like .NET compiler (C# OR VB.net) *does not* translate well the source code into MSIL for the Code 1 where we have a Redirect within the catch:try { Response.Redirect("http://www.google.com"); } catch (Exception ex) { Response.Redirect("http://www.asp.net"); }Is there a way, we can see the MSIL code generated by the .NET compiler for all Code 1,2,3 and JB's case to see. Chances are we may (and only may) have an error at the translation level by the compiler and the error is not caused by ASP.NET. This is just a question only. 4. Other possible causes or explainations are to be seen. Any one else for Catch me if you can? :-Dand where there is a team, there is more than one way.
Phuoc
Participant
1150 Points
230 Posts
Re: Catch me if you can? by try/catch/response.redirect. Need expert to explain.
Jul 31, 2003 05:30 PM|LINK
and where there is a team, there is more than one way.
JBelthoff
Member
553 Points
135 Posts
Re: Catch me if you can? by try/catch/response.redirect. Need expert to explain.
Jul 31, 2003 06:01 PM|LINK
Dodge, Duck, Dip, Dive & Dodge
If a man can dodge a wrench, he can dodge a ball!