首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用Response.Redirect()时获取线程中止异常

使用Response.Redirect()时获取线程中止异常
EN

Stack Overflow用户
提问于 2013-08-27 20:23:29
回答 4查看 48.9K关注 0票数 25

我在Update Panel下的一个页面中编写了以下代码。

protected void myGrid_RowCommand(object sender, GridViewCommandEventArgs e)
{
    try
    {
        if (e.CommandName =="EditNames")
        {
            long lSelectedName = Convert.ToInt64(e.CommandArgument);
            Session["SelectedItem"] = lSelectedName;
            Response.Redirect("EditItem.aspx");
        }
        else if (e.CommandName =="DeleteNames")
        {
            long lSelectedName = Convert.ToInt64(e.CommandArgument);
            ValidName.DeleteItem(lSelectedName);

            ScriptManager.RegisterStartupScript(this, GetType(), "Key", "alert('Name deleted sucessfully')", true);
        }
    }
    catch (System.Threading.ThreadAbortException)
    {

    }
    catch (Exception ex)
    {
        Error handling code...
    }
}

在这里,我得到了一个线程异常,同时重定向。但是,我通过使用错误处理程序System.Threading.ThreadAbortException解决了这个问题。

但我不确定为什么在重定向时会出现这个错误。尽管我解决了这个问题,但我想知道我的编码方式是否有任何错误,或者是否有任何方法可以完全停止错误触发。

给出你的输入...

请注意,该页面位于AJAX更新面板下。

EN

回答 4

Stack Overflow用户

发布于 2014-07-22 14:41:46

请阅读本文-- http://blogs.msdn.com/b/tmarq/archive/2009/06/25/correct-use-of-system-web-httpresponse-redirect.aspx

最好是通过调用Context.ApplicationInstance.CompleteRequest()来绕过请求执行管道,而不是结束请求。

所以你的代码应该是这样的:

Response.Redirect("TargetPage", false);        //write redirect
Context.ApplicationInstance.CompleteRequest(); // end response
票数 35
EN

Stack Overflow用户

发布于 2016-06-25 17:30:52

发生这种情况是因为您在try/catch块中重定向。不要这样做。

票数 2
EN

Stack Overflow用户

发布于 2014-08-02 14:42:43

在重定向方法中提到false就足够了,比如Response.Redirect("TargetPage",false);

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18465334

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档