Error redirecting page

I get an error message:

"The expression cannot be evaluated because the code is optimized or the native frame is on top of the call stack."

when I redirect my page from one to another.

I am using this code:

try
{
    Session["objtwitter"] = obj_UserDetSumit;

           Response.Redirect("TwitterLogin.aspx");


    }
    catch (Exception ex)
    {

          lblStatus.Text = "Account Creation Failed, Please Try Again";


    }

And I have a solution, and I also tried this one. Response.Redirect("home.aspx",false); This is not a mistake, but it also does not redirect the page.

+3
source share
5 answers

Try-catch . , , , ( ). try-catch, , Response.Redirect( " url", false); ThreadAbortException.

+4

: http://msdn.microsoft.com/en-us/library/t9dwyts4.aspx

Response.Redirect Response.End, ThreadAbortException, :

try
{
   Response.Redirect("home.aspx"); 
}
catch(System.Threading.ThreadAbortException)
{
   // do nothing
}
catch(Exception ex)
{
   // do whatever
}
+2

-, , , . -, , ( , ), .

0

Response.Redirect catch try :

try
{
    Session["objtwitter"] = obj_UserDetSumit;
}
catch (Exception ex)
{
    lblStatus.Text = "Account Creation Failed, Please Try Again";
    return;
}

Response.Redirect("TwitterLogin.aspx");

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

, Response.Redirect catch try. Response.Redirect(url); Response.End();, ThreadAbortException.

, - .

Using a Reflector , you can see what Response.Redirect(url);causesResponse.Redirect(url, true);

Passing true then calls Response.End();, which looks like this:

public void End()
{
    if (this._context.IsInCancellablePeriod)
    {
        InternalSecurityPermissions.ControlThread.Assert();
        Thread.CurrentThread.Abort(new HttpApplication.CancelModuleException(false));
    }
    ....
}

Even if you don’t have the final piece of code, it might try to execute "empty", but it cannot, because the stream is interrupted. Just a thought.

0
source

If you replace your code with this line, you will not get the exception in the first place

try
{
   Response.Redirect("home.aspx", False); 
   //Directs the thread to finish, bypassing additional processing
   Context.ApplicationInstance.CompleteRequest();
}
catch(Exception ex)
{
   // exception logging
}

Hope this helps

0
source

Source: https://habr.com/ru/post/1717880/


All Articles