簡體   English   中英

asp.net中的頁面重定向引發的異常

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

我有這個代碼

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

它給出了Response.Redirect("Default.aspx");的異常Response.Redirect("Default.aspx");

例如:線程被中止。

知道為什么嗎?

感謝名單

從Try ... Catch語句中重定向將導致引發此異常,因此這不是您想要執行的操作。

我會將您的代碼更新為;

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

這是ASP.NET在執行重定向時引發的典型異常。 它在Interweb上有很好的記錄。

嘗試使用以下catch塊吞下該異常,並且一切正常。 應該什么都不做!

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