href with Submit on Asp.net core MVC

Asked

Viewed 246 times

0

I have a _Loginpartial view in which you have a link to QUIT (Logout system).

<li role="presentation">
      <a href="javascript:void(0)" role="menuitem">
         <i class="icon wb-power" aria-hidden="true"></i> Sair
      </a>                       
</li>

How do I make him type="submit" and shoot my action asp-action="Logout"?

<form asp-area="" asp-controller="Account" asp-action="Logout" method="post" id="logoutForm" class="navbar-right">
        <ul class="nav navbar-nav navbar-right">
            <li>
                <a asp-area="" asp-controller="Manage" asp-action="Index" title="Manage">Olá @UserManager.GetUserName(User)!</a>
            </li>

            <li class="dropdown">
                <a class="navbar-avatar dropdown-toggle" data-toggle="dropdown" href="#" aria-expanded="false"
                   data-animation="scale-up" role="button">
                    <span class="avatar avatar-online">
                        <img src="../../assets/portraits/5.jpg" alt="...">
                        <i></i>
                    </span>
                </a>
                <ul class="dropdown-menu" role="menu">
                    <li role="presentation">
                        <a href="javascript:void(0)" role="menuitem"><i class="icon wb-user" aria-hidden="true"></i> Perfil</a>
                    </li>
                    <li role="presentation">
                        <a href="javascript:void(0)" role="menuitem"><i class="icon wb-settings" aria-hidden="true"></i> Configurações</a>
                    </li>
                    <li class="divider" role="presentation"></li>
                    <li role="presentation">
                        <a href="javascript:void(0)" role="menuitem"><i class="icon wb-power" aria-hidden="true"></i> Sair</a>                       
                    </li>
                </ul>
            </li>
        </ul>
 </form>

1 answer

0

As you just want to quit the system, there is no need for a Ubmit, just put the href pointing to your method, example:

View

 <a href="~/Login/Logout" role="menuitem">
     <i class="icon wb-power" aria-hidden="true"></i> Sair
 </a>  

Controller

public class LoginController : Controller
{
    public ActionResult Logout()
    {      
        //retorna para a pagina inicial
    }
}
  • Hello Cassio!! It gives displays an msg informing that it was not possible to find the page... I checked the route right but will not.

  • Hello Jalber, so I gave you an example, if you copy my code, you should create the Logout function too, if not, you won’t even find it.

  • It already exists now! It is in the Account controller: [Httppost] [Route("Account/logout")] [Validateantiforgerytoken] public async Task<Iactionresult> Logout() {&#xA; await _signInManager.SignOutAsync();&#xA; _logger.LogInformation("User logged out.");&#xA; return RedirectToAction(nameof(HomeController.Index), "Home");&#xA; }

  • and as your href?

  • <a href="~/Account/logout" role="menuitem"><i class="icon Wb-power" Aria-Hidden="true"></i> Quit</a>

  • Guy has some error on the route, when you click it goes where? which link appears in the address bar?

  • https://localhost:44355/home/Account/logout

  • So that’s the error, put it like this in your href: //Account/logout/

  • Weird. It doesn’t work....

  • It’s going the same way?

  • https://Account/logout/

  • Exchange href for: Url.Action("logout", "Account")

  • It still doesn’t work..... If I do so, it works: <button type="Submit" class="btn btn-link navbar-btn navbar-link">Log out</button>

  • @Jalberromano Your action meets the POST method and you’re using a link (that sends GET)

  • @LINQ and how do I post using the same javascript? <a href="javascript:void(0)" role="menuitem"><i class="icon Wb-power" Aria-Hidden="true"></i> Quit</a>

  • @Jalberromano why you need a post?

  • It is that in my model Identity, created by Asp.net core, is created with this standard Automatically. As I’m using a web theme, I need to adapt it.

Show 12 more comments

Browser other questions tagged

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