Web Dev
Web Dev

Reputation: 735

Why does RedirectToPage() not work in my ASP.NET Core 3 Razor Pages async method?

I have the following code in LogOut razor page.

public async Task OnPostAsync()
{
  await loginManager.SignOutAsync();
  RedirectToPage("/Identity/Login");
}

But it is not redirecting to login page as expected. It still shows LogOut page.

If I use Response.Redirect() instead of RedirectToPage() then it works.

I am using Preview 3 of ASP.NET Core 3.

Upvotes: 6

Views: 9688

Answers (1)

haim770
haim770

Reputation: 49095

The RedirectToPage() method is generating a RedirectToPageResult that you forgot to actually return from your action.

Try this instead:

public async Task<IActionResult> OnPostAsync()
{
    await loginManager.SignOutAsync();

    return RedirectToPage("/Identity/Login");
}

Upvotes: 16

Related Questions