I cannot call an asynchronous method in the view

Asked

Viewed 85 times

0

I’m trying to call this method asynchronous in the view but I can’t.

private async Task LogOff()
{
   RedirectToAction("Index", "Home");
   await HttpContext.SignOutAsync();
}

This is one of the ways I’m trying to call.

<script>
    $(".sair").click(function () {
        $.get("../Login/LogOff", function(data, status) {
             alert("Data: " + data + "\nStatus: " + status);
             });
        });
</script>

The other way I tried to call but it didn’t work.

<a class="nav-link text-dark sair" asp-area="" asp-controller="Login" asp-action="LogOff">Sair</a>
  • Why can’t you do it?

1 answer

1


Use the second way to log out, but adjust your controller code to the following:

public async Task<IActionResult> LogOff()
{
    await HttpContext.SignOutAsync();
    return RedirectToAction("Index", "Home");
}

You must return a view and call the method SignOutAsync before returning it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.