在asp.net中执行Response.End();
方法时抛出了我在catch块中处理的ThreadAbortException
。内部catch块结束后,我想执行一些进一步的代码,但它直接跳到外部catch块。这是因为响应已经结束,.NET框架不执行任何进一步的代码?执行Response.End()方法时在asp.net中处理ThreadAbortException异常
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;
}
}
应该在“下载样品后进行进一步处理”中做了什么?该页面将完成其任务(为下载文件提供服务,您希望下一步做什么? – Alexander
@Alexander:我只发布了我创建的sampel代码来解释我的场景,真正的代码在加载之前做了很多事情,下载currentaly后,我已经做了我们的下载代码之后,我想纠正和移出catch块的catch块。 –
我认为,尽管如此,但我问的原因是,我害怕你使用的页面生命周期错误,因此,对我来说这是一个重要的问题,对于Page,在结束回应之后,生活就结束了 – Alexander