大家知道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的时候,记得你后面的代码还是会执行的,别让它影响到你业务处理,谨慎使用!
关于ASP.NET 解决Response.Redirect ThreadAbortException异常注意事项
发布于: 2013-03-29 | 分类: .net/c#
热门分类
推荐阅读
- .NET程序报错Could not load file or assembly与0x80131040的排查方法(程序集版本绑定失败、bindingRedirect与Fuslogvw定位)
- C#异步调用后界面卡死与请求无响应的排查方法(Task.Result死锁、SynchronizationContext与ConfigureAwait定位)
- C#跨线程访问控件报错Cross-thread operation not valid的解决方法(InvokeRequired、BeginInvoke、Dispatcher与InvokeAsync)
- ASP.NET Core启动报错Unable to resolve service for type的解决方法(依赖注入注册遗漏、生命周期不匹配与循环依赖排查)
- ASP.NET Core部署到IIS报错HTTP Error 500.30与502.5的排查与解决方法(ANCM、stdout日志、应用池配置)