当前位置:首页 > 关于ASP.NET 解决Response.Redirect ThreadAbortException异常注意事项

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

点击次数:1586  更新日期:2013-03-29
大家知道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的时候,记得你后面的代码还是会执行的,别让它影响到你业务处理,谨慎使用!