代码改变世界

关于ASP.NET 解决Response.Redirect ThreadAbortException异常注意事项

2012-07-12 18:44  kaleyroy  阅读(463)  评论(0编辑  收藏  举报

大家知道ASP.NET中Response.Redirect()是用来跳转页面的.关于这个不用过多解释. 当在try catch 块中使用的时候会引发ThreadAbortException异常,这个网上很多都说了解决方案,下面我引用MS官方建议的解决方案.

For Response.End, call the HttpContext.Current.ApplicationInstance.CompleteRequest method instead of Response.End to bypass the code execution to the Application_EndRequest event. For Response.Redirect, use an overload, Response.Redirect(String url, bool endResponse) that passes false for the endResponse parameter to suppress the internal call to Response.End.

For example:

      Response.Redirect ("nextpage.aspx", false);
                            

    If you use this workaround, the code that follows Response.Redirect is executed.
    For Server.Transfer, use the Server.Execute method instead.

大家注意到红底字部分,人家说明了如果设置false的话:

Response.Redirect ("nextpage.aspx", false);

后面的代码还会继续执行.所以我们应该注意到这一点!

 

所以我们:

1.当跳转的时候希望完全截断后面的代码,建议Response.Redirect 放到try catch外面使用,或者使用别的更好的方式!

2.当使用Response.Redirect设置为false的时候,记得你后面的代码还是会执行的,别让它影响到你业务处理,谨慎使用!

 

PS:做个备忘录,以备不时之需.

Make a memo, for a rainy day