zoukankan      html  css  js  c++  java
  • 调用Response.Redirect, Server.Transfer遇到ThreadAbortException

    开发环境为VS2005,OS 为Windows 2003,系统登录后在跳转到另一页面时会报此错误:
    在 System.Threading.ThreadAbortException 中第一次偶然出现的“mscorlib.dll”类型的异常
    “System.Threading.ThreadAbortException”类型的异常在 mscorlib.dll 中发生,但未在用户代码中进行处理
    但不影响程序的正常运行。于是在网上查了查,发现相关资料不多。后来找到微软的官方解释,搞定。
    --------------------------------------------------------------------------------------------------------------

    症状

    如果使用 Response.EndResponse.Redirect Server.Transfer 方法,则出现 ThreadAbortException 异常。 可使用 try-catch 语句捕捉此异常。

    原因

    Response.End 方法停止页的执行,并将该执行变换到应用程序的事件管线中的 Application_EndRequest 事件。 Response.End 后面的代码行将不执行。

    此问题出现在 Response.Redirect Server.Transfer 方法中,这是由于这两种方法都在内部调用 Response.End

    解决方案

    若要解决此问题,请使用下列方法之一:
    • 对于 Response.End,调用 ApplicationInstance.CompleteRequest 方法而不调用 Response.End,以便跳过 Application_EndRequest 事件的代码执行。
    • 对于 Response.Redirect,使用重载 Response.Redirect(String url, bool endResponse),对 endResponse 参数它传递 false以取消对 Response.End 的内部调用。例如:
        Response.Redirect ("nextpage.aspx", false);
      如果使用这种解决方法,Response.Redirect 后面的代码将得到执行。
    • 对于 Server.Transfer,请改用 Server.Execute 方法。

    状态

    这种现象是设计使然。
  • 相关阅读:
    获取目录下所有文件名
    毕业论文endnote使用
    CoinChange
    sublime3个人配置
    2015-12-31
    2015-12-09
    #define DEBUG用法
    fiddler介绍
    app测试模块
    android SDK_安装配置_使用
  • 原文地址:https://www.cnblogs.com/twilight/p/1280729.html
Copyright © 2011-2022 走看看