I am trying to add async features to an existing asp.net forms application. But I am running into problems with page transfer/redirection. Reason being in much of the existing code redirects/transfers are made from utility classes or custom controls etc. Also, have code that exists after the redirect/transfer further up the stack that needs to be avoided. Original code took advantage of redirect/transfer internally calling Response.End and short circuiting out of the code execution for the thread via the ThreadAbortException.
For Server.Transfer and Response.Redirect, pages that have been made asynchronous by adding Async="true" have the ThreadAbortException bubble up and the browser ends up with an error response. I am assuming that since the page is async the current page request finishes before the transfer page, instead of the sync way of thread abort, then would start processing the transfer page.
When I use Response.Redirect( "[somepagename]", false) or Server.Execute, they prevent the current response from ending so I add ApplicationInstance.CompleteRequest after them, which skips later event and handler pipeline processing. But code after the redirect/execute continues which causes other errors/issues. (For example calling to Response.Redirect again to go to a different page based on logic that would have been skipped originally by the ThreadAbortException.)
I also tried Response.FlushAsync between Server.Execute and ApplicationInstance.CompleteRequest which looks ok to the browser. Exceptions after CompleteRequest don't show to the browser (at least in dev) since FlushAsync sends the buffered response from the transfer page. But that still might have race condition as to if the flush or continued code execution of the first page finish first. And subsequent code after CompleteRequest is still executed which may have side effects.
Original code:
public static void GoToPage( string page, bool transfer )
{
if ( transfer ){HttpContext.Current.Server.Transfer( page, false ); }
else { HttpContext.Current.Response.Redirect( page ); }
}