• asp.net 在使用Response.Redirect try{}catch{}块失效


    asp.net 在使用Response.Redirect try{}catch{}块失效

     

    try
    {


    Response.Redirect(
    "/mymaimai.aspx");

    }
    catch (Exception e)
    {
    //
    异常处理

    }


    使用以上语句,不管是否有异常,都会执行catch中的,一直显示""失败"",都会抛出System.Threading.ThreadAbortException,原因如下
    :
    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 (
    "/mymaimai.aspx", false);如果使用这种解决方法,Response.Redirect 后面的代码将得到执行。
     
    对于 Server.Transfer,请改用 Server.Execute 方法。
     
    状态

    这种现象是设计使然。

    解决后的代码
    :
    try
    {
    Response.Redirect(
    "/mymaimai.aspx"
    ,false);
    }
    catch 
    {
    //
    异常处理

    }
     

     Response.Redirect 与 异常(线程正在中止)
    如果使用 try 、catch 处理包含Response.Redirect  语句代码,总是能捕捉到异常:线程正在中止,
    其实不仅仅Response.Redirect 会,Response.End 和 Server.Transfer 也是一样的情况,本质的原因的原因是Response.End 方法停止页的执行,并将该执行变换到应用程序的事件管线中的 Application_EndRequest 事件, Response.End 后面的代码行将不执行。通常认为Response.End 方法是线程的非正常结束,因此MS就在内部抛出ThreadAbortException 异常,而Response.Redirect 和 Server.Transfer 这两种方法都在内部调用 Response.End。


    症状
    如果使用 Response.End、Response.Redirect 或 Server.Transfer 方法,将出现 ThreadAbortException 异常。您可以使用 try-catch 语句捕获此异常。
     
    原因
    <!-- Inject Script Filtered -->

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

    此问题出现在 Response.Redirect 和 Server.Transfer 方法中,因为这两种方法均在内部调用 Response.End。
     
    解决方案
    要解决此问题,请使用下列方法之一:   对于 Response.End,调用 HttpContext.Current.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 方法。


    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/High_Mount/archive/2010/04/02/5443311.aspx

  • 相关阅读:
    [大山中学模拟赛] 2016.9.17
    [DP优化方法之斜率DP]
    Gengxin讲STL系列——String
    小班讲课之动态规划基础背包问题
    ubuntu安装体验
    小班出题之字符串基础检测
    G
    B
    小项目--反eclass
    树--天平问题
  • 原文地址:https://www.cnblogs.com/lavenderzh/p/1772039.html
Copyright © 2020-2023  润新知