Response.Redirect投掷错误

时间:2011-11-02 14:12:11

标签: c# asp.net

我遇到了response.redirect调用的问题。

错误:

  

System.Threading.ThreadAbortException:线程正在中止。在   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:\构建\ 15个\数码\主\来源\网络\公用\ LoginHandler.aspx.cs:行   113

重定向发生在try - catch声明中,我似乎无法找到正确的方法。

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

3 个答案:

答案 0 :(得分:14)

尝试使用此技术:

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

这应该避免ThreadAbortException,但仍然可以完成请求。

答案 1 :(得分:0)

你可以说Response.Redirect(“home.aspx”, false);并且它不会停止请求。

但它会继续执行。使用Response.Redirect(“home.aspx”, false);

时要小心

如果你传递了错误,你将不会收到错误,但它不会结束请求

如果我错了,请纠正我。但像这样的代码

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

即使count == 0

Prospect.Save(-1, purchaceDate); 

将永远运行。当你可能期望它停止执行时,它会保存一个新的潜在客户

答案 2 :(得分:0)

您应该使用以下

Response.Redirect("URL", false);
相关问题