【问题标题】:Response.Redirect throwing errorResponse.Redirect 抛出错误
【发布时间】:2011-11-02 14:12:10
【问题描述】:

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

错误:

System.Threading.ThreadAbortException:线程被中止。在 System.Threading.Thread.AbortInternal() 在 System.Threading.Thread.Abort(对象状态信息)在 System.Web.HttpResponse.End() 在 System.Web.HttpResponse.Redirect(String url, Boolean endResponse) 在 System.Web.HttpResponse.Redirect(String url) 在 Web.AdminUser.LoginHandler.OpenIdLogin() 在 c:\Builds\15\Digital\main\Sources\Web\Public\LoginHandler.aspx.cs:line 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);
        }

【问题讨论】:

  • 如果不看实际代码,很难找出问题所在
  • 你在 LoginHandler.aspx.cs 中看起来怎么样,尤其是在第 113 行的区域?

标签: c# asp.net


【解决方案1】:

尝试使用这种技术:

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

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

【讨论】:

    【解决方案2】:

    你应该使用下面的

    Response.Redirect("URL", false);
    

    【讨论】:

      【解决方案3】:

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

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

      如果你传递 false 你不会得到错误但它不会结束请求

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

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

      即使计数 == 0

      Prospect.Save(-1, purchaceDate); 
      

      将始终运行。并且会在您期望它停止执行时保存新的潜在客户

      【讨论】:

      • AFAIK,这不会发生,将被终止或继续的是响应流本身,而不是执行的代码,所以上面的代码不会命中“Prospect.Save()”方法如果重定向发生了。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-28
      • 1970-01-01
      • 1970-01-01
      • 2015-03-24
      • 2011-12-27
      • 2018-08-06
      相关资源
      最近更新 更多