简体   繁体   English

asp.net中的页面重定向引发的异常

[英]Exception thrown by page redirection in asp.net

I have this code 我有这个代码

protected void Button_Click(object sender, EventArgs e)
{
    try
    {
        // some code

        con.Open();
        string result = command.ExecuteScalar().ToString();

        if (result != string.Empty)
        {
             // some code
             Response.Redirect("Default.aspx");
        }
    }
    catch (Exception ex)
    {
        throw new Exception(ex.Message);
    }
    finally
    {
        con.Close();
    }

It gives an exception from Response.Redirect("Default.aspx"); 它给出了Response.Redirect("Default.aspx");的异常Response.Redirect("Default.aspx");

ex: Thread was being aborted. 例如:线程被中止。

any idea why? 知道为什么吗?

thanx 感谢名单

Redirecting from within a Try...Catch statement will result in this Exception being thrown, so this is not what you want to do. 从Try ... Catch语句中重定向将导致引发此异常,因此这不是您想要执行的操作。

I would update your code to; 我会将您的代码更新为;

string result = string.Empty;

try
{
    // some code
    con.Open();
    result =  command.ExecuteScalar().ToString();        
}
catch (Exception ex)
{
    throw new Exception(ex.Message);
}
finally
{
    con.Close();
}

if (result != string.Empty)
{
     // some code
     Response.Redirect("Default.aspx");
}

This is a typical Exception that is thrown by ASP.NET when performing a redirect. 这是ASP.NET在执行重定向时引发的典型异常。 It's quite well documented on the Interweb. 它在Interweb上有很好的记录。

Try the following catch block to swallow the exception and all should be fine. 尝试使用以下catch块吞下该异常,并且一切正常。 It's supposed to do nothing! 应该什么都不做!

catch(ThreadAbortException)
{
}
catch (Exception ex)
{
    throw new Exception(ex.Message);
}
finally
{
    con.Close();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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