简体   繁体   中英

Response.Redirect throwing error

I am having an issue with a response.redirect call.

Error:

System.Threading.ThreadAbortException: Thread was being aborted. at System.Threading.Thread.AbortInternal() at System.Threading.Thread.Abort(Object stateInfo) at System.Web.HttpResponse.End() at System.Web.HttpResponse.Redirect(String url, Boolean endResponse) at System.Web.HttpResponse.Redirect(String url) at Web.AdminUser.LoginHandler.OpenIdLogin() in c:\\Builds\\15\\Digital\\main\\Sources\\Web\\Public\\LoginHandler.aspx.cs:line 113

The redirect is happening in a try - catch statement and I can't seem to figure out the right way to do it.

try
        {
            if (Request.Form.HasKeys())
            {
                Global.Logger.Info(string.Format("OpenIdLogin_Has_Keys"));

                string request = Request.Form.GetValues("token")[0].ToString();

                Rpx rpx = new Rpx("123412341234", "https://login.youwebsite.com/");


                var xml = rpx.AuthInfo(request).InnerXml;

                //lblx.Text = xml.ToString();
                XElement xdoc = XElement.Parse(xml);

                if (xdoc.Element("email") != null)
                    xdoc.Element("email").Value = "";


                int userId = SaveMember(xdoc);
                if (userId > -1)
                {
                    //add the user id to session for later
                    Session["CurrentUserId"] = userId;
                    Session["UserLoggedIn"] = true;
                }
                else
                {
                    Session["UserLoggedIn"] = false;
                }

                articlePath = String.Format(articlePath, Section, Name);
                Response.Redirect(articlePath, false);
            }
        }
        catch (Exception e)
        {
            Global.Logger.Error(e);
            articlePath = String.Format(articlePath, Section, Name);
            Response.Redirect(articlePath, false);
        }

Try using this technique:

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

This should avoid the ThreadAbortException , but still complete the request.

you can say Response.Redirect(“home.aspx”, false); and it will not stop the request.

but it will continue to execute. so careful when using Response.Redirect(“home.aspx”, false);

If you pass false you will not get the error but it will NOT end the request

correct me if I'm wrong. But code like this

public void Btn_Click() 
{
    if(count == 0)
    {
          Response.Redirect("OutOfStock.aspx", false);
    }
    Prospect.Save(-1, purchaceDate);
}

Even if count == 0

Prospect.Save(-1, purchaceDate); 

will always run. And will save a new prospect when you might expect it to stop execution

你应该使用以下

Response.Redirect("URL", false);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM