C# – Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack error message

asp.netc

whenever i Response.Redirect("myexamplepage.aspx"); it either keep looping and then lastly show the page is unable to view or show this above error. However, if i Response.Redirect("http://www.google.com.sg"); it works.

I tried all the methods i found online such as the following:

  1. Response.Redirect(url,false)
  2. Redirecting url outside the try-catch

I'd also make sure that my project is running debug mode.

BUT i am still facing this problem. It is like so random because previously i dont have any problems redirecting to my url. Please provide me with solutions thanks.

Best Answer

This error occures most of the time when you are trying to redirect to a new page, while the current page is still trying to complete it's proces.

You can find more information about the issue right here:

http://support.microsoft.com/kb/312629/EN-US/

So change your code to the following:

HttpContext.Current.ApplicationInstance.CompleteRequest();
Response.Redirect ("myexamplepage.aspx", false);