I am working on an older ASP.Net Web Forms application. I am looking to introduce async code to help modernise it. However have encountered an impase.
The application make uses of Response.Redirect(true)
in the Master Page to end a session when doing things like checking whether the user is logged in. This ends the response and redirects user back to the login page.
The trouble is when async code is used on the page. Eg:
protected async void Page_Load(object sender, EventArgs e)
{
await _userService.GetUser();
if (!IsPostBack)
{
...
}
}
The page blows up when the redirect is hit in the master page with: System.Threading.ThreadAbortException: Thread was being aborted.
I understand why this happens from this question: Why Response.Redirect causes System.Threading.ThreadAbortException? but I am unsure how to get around this issue. I would like to use async code in the page and also allow the Response.Redirect to end the response without processing the rest of code.
I tried to use:
Response.Redirect(url, false);
Context.ApplicationInstance.CompleteRequest();
But then the page execution continues. Is there any way around this issue on WebForms? Or do I need to revert to non async code? There does not seem to be a way like in MVC to return RedirectToAction
which stops page execution?
From my understanding it seems like async support is available for WebForms - how are other projects getting around this kind of issue? especially in scenarios where we need to stop page execution (for instance if user is logged out?)