执行Response.End()方法时在asp.net中处理ThreadAbortException

Nee*_*pta 8 .net c# asp.net

Response.End();在asp.net中执行方法时,它抛出ThreadAbortException我在catch块中处理的内容,在内部catch块结束后我想执行一些其他代码,但它直接跳转到外部catch块.这是因为响应已经结束而且.net框架不执行任何进一步的代码?

protected void btn_click(object sender, EventArgs e)
{

    try
    {
        string fileToDownload = MapPath(@"~\Sample.txt");
        string fileToRead = MapPath(@"~\FileNotExist.txt");

        try
        {
            //Section 1
            try
            { 
                // try to read the file which does not exist to raise the exception
                StreamReader ss = new StreamReader(fileToRead);
            }
            catch (IOException IoEx)
            {
                // Just for sample exception
            }

            // Section 2 code block still execute because exception handled by upper try catch block 
            //Section 2

            Response.Clear();
            Response.ClearHeaders();
            Response.AddHeader("Content-Disposition", "attachment;filename=SampleTemplate.txt");
            Response.ContentType = "text";
            Response.WriteFile(fileToDownload);
            Response.Flush();
            Response.End();

        }
        catch (System.Threading.ThreadAbortException abrtEx)
        {
          // do not treat this exception as Exception
        }

        //Section 3 Code block not executing even after exception handeled by ThreadAbortException 
        //Section 3
         string test = "Do futher process after sample downloaded";


    }
    catch (Exception ex) // Outer Catch Block
    {
        throw ex;
    }


}
Run Code Online (Sandbox Code Playgroud)

Vig*_*r A 7

代替

Response.End()

使用

HttpContext.Current.ApplicationInstance.CompleteRequest()

像这样

protected void btn_click(object sender, EventArgs e)
{
    try
    {
        string fileToDownload = MapPath(@"~\Sample.txt");
        string fileToRead = MapPath(@"~\FileNotExist.txt");

        try
        {
            //Section 1
            try
            { 
                // try to read the file which does not exist to raise the exception
                StreamReader ss = new StreamReader(fileToRead);
            }
            catch (IOException IoEx)
            {
                // Just for sample exception
            }

            // Section 2 code block still execute because exception handled by upper try catch block 
            //Section 2

            Response.Clear();
            Response.ClearHeaders();
            Response.AddHeader("Content-Length", fileToDownload.Length.ToString()); 
            Response.AddHeader("Content-Disposition","attachment;filename=SampleTemplate.txt");
            Response.ContentType = "text";
            Response.WriteFile(fileToDownload);
            Response.Flush();
            HttpContext.Current.ApplicationInstance.CompleteRequest();

        }
        catch (System.Threading.ThreadAbortException abrtEx)
        {

        }

        //Section 3 Code block not executing even after exception handeled by ThreadAbortException 
        //Section 3
         string test = "Do futher process after sample downloaded";


    }
    catch (Exception ex) // Outer Catch Block
    {
        throw ex;
    }
}
Run Code Online (Sandbox Code Playgroud)


Jam*_*mil 5

根据PRB:如果您使用Response.End,Response.Redirect或Server.Transfer,则会发生ThreadAbortException:

如果使用Response.End,Response.RedirectServer.Transfer方法,则会发生ThreadAbortException异常.您可以使用try-catch语句来捕获此异常.

到Response.End方法结束的页面执行和转移执行到Application_EndRequest在应用程序的事件管线事件.不执行Response.End之后的代码行.

Response.RedirectServer.Transfer方法中会出现此问题,因为两个方法都在内部调用Response.End.

解决这个问题

,使用以下方法之一:

对于Response.End,调用 HttpContext.Current.ApplicationInstance.CompleteRequest方法而不是Response.End来绕过代码执行到 Application_EndRequest事件.

对于Response.Redirect,使用一个重载,Response.Redirect(String url,bool endResponse),它为endResponse参数传递false以禁止对Response.End的内部调用.例如: Response.Redirect("nextpage.aspx",false); 如果使用此变通方法,则执行Response.Redirect之后的代码.

对于Server.Transfer,请改用Server.Execute方法.

此行为是设计使然.