Why Response.Redirect causes System.Threading.ThreadAbortException?

前端 未结 10 1726
误落风尘
误落风尘 2020-11-22 15:00

When I use Response.Redirect(...) to redirect my form to a new page I get the error:

A first chance exception of type \'System.Threading.ThreadAbortEx

10条回答
  •  粉色の甜心
    2020-11-22 15:15

    Also I tried other solution, but some of the code executed after redirect.

    public static void ResponseRedirect(HttpResponse iResponse, string iUrl)
        {
            ResponseRedirect(iResponse, iUrl, HttpContext.Current);
        }
    
        public static void ResponseRedirect(HttpResponse iResponse, string iUrl, HttpContext iContext)
        {
            iResponse.Redirect(iUrl, false);
    
            iContext.ApplicationInstance.CompleteRequest();
    
            iResponse.BufferOutput = true;
            iResponse.Flush();
            iResponse.Close();
        }
    

    So if need to prevent code execution after redirect

    try
    {
       //other code
       Response.Redirect("")
      // code not to be executed
    }
    catch(ThreadAbortException){}//do there id nothing here
    catch(Exception ex)
    {
      //Logging
    }
    

提交回复
热议问题