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?

7
  • 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. Mar 10, 2013 at 0: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, 2013 at 0:41
  • @OlegOrlov See this answer, and code stackoverflow.com/a/14641145/159270
    – Aristos
    Mar 10, 2013 at 0: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, 2013 at 0:46
  • @OlegOrlov I have type the code that ignore this exception, and on the link I explain some more about that.
    – Aristos
    Mar 10, 2013 at 0:47

3 Answers 3

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");
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

8
  • 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, 2013 at 0: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, 2013 at 0:57
  • Event with Tim's solution it isn't redirecting, upper I've posted the variant of project with bools, you can look. 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, 2013 at 1: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, 2013 at 1:23
  • yes, I'm sure, because both methods ( Tim's and yours ) aren't redirecting to the needed page.
    – Secret
    Mar 10, 2013 at 1: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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.