线程中止异常。从邮件页面到注销页面

时间:2017-02-17 04:40:11

标签: c# asp.net webforms

以下是我的代码。这与会话处理运行良好。但问题是当我点击母版页上的注销按钮时出现错误:

  

System.Threading.ThreadAbortException:线程正在中止。

如果我从主页点击退出按钮,如何redirect返回登录页面需要帮助..

以下代码来自我的主aspx文件,其中包含masterpage

  protected async void Page_Load(object sender, EventArgs e)
    {
        if (Session["mail"] == null)
        {

            Response.Redirect("~/login.aspx", false);
            Context.ApplicationInstance.CompleteRequest();
        }
        else
        {

            await Task.Run(() => retrivelast());
         }
     }

以下是注销事件母版页中的代码

    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        Session.Abandon();
        HttpContext.Current.Session.Clear();
        HttpContext.Current.Session.Abandon();
        HttpContext.Current.Response.Cookies.Add(new HttpCookie("mail",                ""));
        Response.Redirect("login.aspx");
    }

linkbutton2_click是我的退出事件。

1 个答案:

答案 0 :(得分:0)

尝试用{替换Response.redirect("login.aspx"); 以下内容:

Response.Redirect("login.aspx", false);
HttpContext.Current.ApplicationInstance.CompleteRequest();
Visible = false;

您的问题是默认重定向调用另一个抛出ThreadAbortException的方法。

有关此方法的更多信息(以及更通用的解决方案),请参阅:

https://www.codeproject.com/tips/561490/asp-net-response-redirect-without-threadabortexcep

另一个选择是捕获ThreadAbortException。