1

Have trouble with the Response.Redirect() and getting the error:

Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack

I've googled and found some topics here, like: Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack

Where people are offering to set the 2nd argument to the false value to make it work, like:

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

But as for my program it isn't redirecting to the new one... It just continues to stay at this page.

I've done a breakpoint on other page, which to I want to redirect. But there wasn't any event for the breakpoint to catch. Looks like it just doesn't send the request to another page.

My code is rather big, so I've posted the code not here, but at ideone: http://ideone.com/bQzCJd

The Redirect() method is on the 57th line, which:

  • occurs an exception, if the 2nd argument is set to true
  • nothing happen, if is set to false

What must I do to fix it?

Community
  • 1
  • 1
Secret
  • 2,627
  • 7
  • 32
  • 46
  • Why don't you use `Response.Redirect("PageName.aspx")` but after the `try/catch`? Use a `bool` variable e.g. `mustRedirect` which you can check before the redirect. – Tim Schmelter Mar 10 '13 at 00:39
  • @TimSchmelter Because in future I want to make the redirection to the several pages. It may redirect to the Personal Cabinet, it may catch the notification, that such IP is banned from DB and I must to redirect to some forbidden area etc... – Secret Mar 10 '13 at 00:41
  • @OlegOrlov See this answer, and code http://stackoverflow.com/a/14641145/159270 – Aristos Mar 10 '13 at 00:42
  • @Aristos thanks for the interesting info about hacking issue, if is set to false value, but in my code exactly if I set to the `true` I'm getting such an exception. How could I fix it? – Secret Mar 10 '13 at 00:46
  • @OlegOrlov I have type the code that ignore this exception, and on the link I explain some more about that. – Aristos Mar 10 '13 at 00:47
  • @OlegOrlov: Even if you want to redirect to several pages you could use several `bools`. – Tim Schmelter Mar 10 '13 at 00:54
  • @TimSchmelter pity... but it doesn't redirect even with the bools... Could you please look at my project, I've posted the VS-project here (requires vs2010 ): http://81.176.229.82/WebApplication1.zip – Secret Mar 10 '13 at 01:01

3 Answers3

1

occurs an exception, if the 2nd argument is set to true

Yes, a ThreadAbortException because you redirect in a Try-Catch.

Why don't you use Response.Redirect("PageName.aspx") but after the try/catch? Use a bool variable e.g. redirectCabinet which you can check before your redirect:

bool redirectCabinet = false;
try
{
    // ...
    if (tmpStr != String.Empty)
    {
        if (pageHandler.Request.HttpMethod == "GET")
        {
            if (pageHandler.Request.Params["method"] == "checkAuth")
            {
                // ...
                bool userExists = Convert.ToBoolean(mysqlObject.MakeScalar(ref mysqlConn,
                if (userExists)
                {
                   // ...
                    redirectCabinet = true;
                    //Response.Redirect("Cabinet.aspx");
                }
                // ...
            }
        }
        //....
    }
} catch (Exception exc)
{
    exc.ToString();
}

if(redirectCabinet)
    Response.Redirect("Cabinet.aspx");
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
1

I suggest you to avoid the Abort Exception, and use the true to end the execution and move to the next page.

try
{
    // this is throw the ThreadAbortException exception
    Response.Redirect("SecondEndPage.aspx", true);
}
catch (ThreadAbortException)
{
    // ignore it because we know that come from the redirect
}
catch (Exception x)
{

}

Similar : https://stackoverflow.com/a/14641145/159270

Community
  • 1
  • 1
Aristos
  • 66,005
  • 16
  • 114
  • 150
  • thank you, but how will I redirect if there is a catch-block. I just will be caught in catch (ThreadAbortException) { } block and nothing will happen... But I need to fix the redirection process exactly. – Secret Mar 10 '13 at 00:55
  • @OlegOrlov Is not that, the redirect with true is send the redirect command and stop the execution. Try it. This code works :) you may need some return on the function, maybe there on the ThreadAbortException. Tim solution is similar, with a flag. – Aristos Mar 10 '13 at 00:57
  • Event with Tim's solution it isn't redirecting, upper I've posted the variant of project with bools, you can look. http://81.176.229.82/WebApplication2.zip I've prepared the variant which you have suggested... but it also isn't redirecting and I don't understand 'why?', I don't want to use JavaScript for the redirection, because it would be bad for the application. – Secret Mar 10 '13 at 01:08
  • @OlegOrlov I see your code, are you sure that is called ? I do not see anything there to make it not redirect. – Aristos Mar 10 '13 at 01:23
  • yes, I'm sure, because both methods ( Tim's and yours ) aren't redirecting to the needed page. – Secret Mar 10 '13 at 01:34
  • I'm using AJAX also in my page. Could it be a problem for the non-redirecting. Because I've tried also to call JS-function to redirect with the `Page.ClientScript.RegisterStartupScript()` and no success... But JS piece of code can't be wrong, because it's just too simple. Don't know maybe to try Server.Transfer() will be the exit from this situation... – Secret Mar 10 '13 at 01:44
  • @OlegOrlov The redirect is not working is you call it from any ajax. – Aristos Mar 10 '13 at 10:01
  • any AJAX ( as any JS-script ) isn't working also in that place as I said before. I've don't problem with erasing AJAX own scripts and using postbacks with tag. But I don't like this way... I want to know, why did I have problem here... – Secret Mar 10 '13 at 10:34
0

Even with the solutions presented in the comments, it is not working as it should but if redirected with true endRequest, it should work.

Like this:

if(redirectCabinet)
    Response.Redirect("Cabinet.aspx",true);
codingEnthusiast
  • 3,800
  • 2
  • 25
  • 37