Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 执行Response.End()方法时asp.net中的ThreadAbortException处理_C#_Asp.net_.net - Fatal编程技术网

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

C# 执行Response.End()方法时asp.net中的ThreadAbortException处理,c#,asp.net,.net,C#,Asp.net,.net,执行Response.End()时方法,它抛出我在catch块中处理的ThreadAbortException,在内部catch块结束后,我想执行一些进一步的代码,但它直接跳转到外部catch块。发生这种情况是因为响应已结束,.net framework不执行任何进一步的代码吗 protected void btn_click(object sender, EventArgs e) { try { string fileToDownload = MapPath(

执行
Response.End()时方法,它抛出我在catch块中处理的
ThreadAbortException
,在内部catch块结束后,我想执行一些进一步的代码,但它直接跳转到外部catch块。发生这种情况是因为响应已结束,.net framework不执行任何进一步的代码吗

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


}

这是因为你没有打电话给你的捕手。如果没有它,CLR将无法继续执行此方法。因此,您的代码应该是:

try
{
   ...
}
catch (System.Threading.ThreadAbortException abrtEx)
{
   Thread.ResetAbort();
}
但这不是一个好的做法。你可以在这里读到为什么它是有害的-

你可以完成你的逻辑,然后在它调用Error,Enter()之后,而不是在方法

< P>中,而不是

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;
    }
}
根据:

如果使用响应.End响应.Redirect,或 Server.Transfer方法时,出现ThreadAbortException异常。可以使用try-catch语句捕获此异常

Response.End方法结束页面执行并移动 执行应用程序中的应用程序\u EndRequest事件 事件管道。Response.End后面的代码行不正确 执行

此问题出现在响应中。重定向Server.Transfer方法,因为这两种方法都调用Response.End内部

来解决这个问题

,请使用以下方法之一:

有关响应。结束,请调用 HttpContext.Current.ApplicationInstance.CompleteRequest方法,而不是Response.End,以绕过代码执行到 应用程序请求事件

对于Response.Redirect,使用重载Response.Redirect(字符串 url,bool endResponse)为endResponse传递false 参数以抑制对响应的内部调用。结束。对于 示例:Response.Redirect(“nextpage.aspx”,false) 如果使用此解决方法,将执行Response.Redirect后面的代码

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

这种行为是故意的


它的工作,但“Sample.txt”文件内容得到编辑与页面apsx内容一些额外的内容自动添加到我的Sample.txt文件,这似乎连接页面aspx代码。我的样本文件是我的样本。txt文件中有一个只有“名称”的文件,我的样本文件是我的样本。我的样本文件是我的样本文件中的一个文件,我的样本文件是一个只有“名称”的文件,我的样本。我的样本文件是我的样本。我的样本文件是我的样本。我的样本文件是一个只有只有只有“名称”的摩摩摩摩托托托托托亚州州州州的一个,但下载后下载下载后它包含下载后它下载后它下载后它包含的下载后它包含有“名称”在下载后,在下载后的各各各各各各各各毫毫毫毫毫毫毫毫毫毫毫毫毫毫毫毫毫无无无无无无无无无无电电电电电电电日日日日日毫毫毫毫毫毫毫毫毫毫毫毫毫毫毫毫毫毫毫毫毫毫毫毫无无无无无无无无无无无无无无无无无无无无。。。。您的解决方案也在运行,但“Sample.txt”文件内容将通过页面apsx内容进行编辑。一些额外的内容将自动添加到我的Sample.txt文件中,此连接看起来像页面aspx代码。我的样本文件是我的样本文件,我的样本文件是一个只有“名称”的样本文件,我的样本文件是一个只有“名称”的样本文件,我的样本文件是我的样本文件,我的样本文件是一个只有“名称”的文件,但在下载后它包含下载后,下载后它下载后它下载后包含有“名称”但在下载后它下载后它下载后它下载后它下载后它包含的下载后它包含的名称、摩摩摩摩摩托托托托托日日日日毫毫毫毫毫无无无无无无无无无无无无无电电电日日日日日日日无无无无无无无无无无无无无无无无无无无无无无无无无无无无无无无无无无无无无无无无无无无无无日日日日日日日无无无无无日无无无日无无无无日无日无日无无无日无无日无无无无无无无无您好,您需要有内容长度的addheader。我已经更新了解决方案。请检查一下。我也有同样的问题。请参阅这里的stackoverflow.com/questions/20605705/getting-Undored-output-when-using-response-binarywritemybyte-and-context-appl@VigneshKumar在示例文件中ToDownload.Length将返回字符串变量的长度和路径,而不是内容的长度在“下载示例后执行进一步的过程”中应该执行什么操作?页面将完成其任务(提供下载文件。你想让它下一步做什么?@Alexander:我只发布了我创建的sampel代码来解释我的场景,真实的代码在加载之前和下载之后做了很多事情。目前我已经在catch块下完成了我们的after doenload代码,我想更正并移出catch块。我也这么想,是的,但是我问的原因是,我担心您使用的页面生命周期是错误的。因此,对我来说,这是一个重要的问题。对于页面,在您结束响应后,它的生命就结束了。是的,我理解,但在下载代码后,与页面生命周期无关,它激活/激活了一些服务,并编写了从一些网站收到的一些内容其他来源。我有其他的解决方法,但首先,如果可以的话,我会尝试更正代码