0

So the problems stands like this, I have a top navbar which has a logout link like this

<ul class="navbar-nav ml-auto">
                @if (signInManager.IsSignedIn(User))
                {
                    <li class="nav-item">
                        <form method="post" asp-controller="account" asp-action="logout">
                            <button type="submit" style="width:auto"
                                    class="nav-link btn btn-link py-0">
                                Logout @User.Identity.Name
                            </button>
                        </form>
                    </li>
                }

                else
                {
                    <li class="nav-item">
                        <a class="nav-link" asp-controller="account" asp-action="register"> Register</a>
                    </li>
                }
            </ul>

My project also includes areas so I have 2 areas Broker/Customer :

enter image description here

After login with a broker the form action for logout will modify to this

enter image description here

action: /Broker/account/logout, this will cause a problem because i don't have that controller in my Broker area

The question: why it is not showing a /account/logout action like it is stated in my form "post" code: form method="post" asp-controller="account" asp-action="logout"

Account Controller is not defined in any area, it belongs to default Controllers folder as you can see on the image and that AccountController / Logout action will just redirect to HomeController / Index action.

Please help me understand :D

EDIT: MY ROUTING :

app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
               name: "default",
               pattern: "{area:exists}/{controller=Account}/{action=Login}/{id?}");

            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Account}/{action=Login}/{id?}");
            endpoints.MapRazorPages();
        });
4
  • 1) What is your route config ? 2) Do you have a tag [Area("Broker")] in your AccountController ? 3) What is your redirect code ? 4) Do you have any controller defined in your areas ? Commented Sep 6, 2020 at 10:33
  • 2) Do you have a tag [Area("Broker")] in your AccountController ? = "Broker" Tag is present in Broker/HomeController, that AccountController is separate, it does not belong to any area 3) What is your redirect code ? : don't have anything defined 4) Do you have any controller defined in your areas ? yes, there is Broker/Controllers/HomeController Updated post with my routing Commented Sep 6, 2020 at 10:45
  • First problem I see is that you can't have 2 routes defined with the same name. Does it matter with an uppercase ? asp-controller="Account" asp-action="Logout" asp-area="" Commented Sep 6, 2020 at 10:55
  • Because you did not define asp-area in your form tag, it will maybe take the one found in your current route (and because you are on /broker, a default area will be selected). Try to set an empty asp-area Commented Sep 6, 2020 at 11:01

2 Answers 2

2

Found the solution, in my AccountController, which does not belong to any area, just use route attribute [Route("/Account/Logout")], in this way the form post action is generated correctly enter image description here

 [HttpPost]
    [Route("/Account/Logout")]
    public async Task<IActionResult> Logout()
    {
        await _signInManager.SignOutAsync();
        return RedirectToAction("login", "account");
    }

So html stays untouched

 <ul class="navbar-nav ml-auto">
                @if (signInManager.IsSignedIn(User))
                {
                    <li class="nav-item">
                        <form method="post" asp-controller="account" asp-action="logout">
                            <button type="submit" style="width:auto"
                                    class="nav-link btn btn-link py-0">
                                Logout @User.Identity.Name
                            </button>
                        </form>
                    </li>
                }

                else
                {
                    <li class="nav-item">
                        <a class="nav-link" asp-controller="account" asp-action="register"> Register</a>
                    </li>
                }
            </ul>
Sign up to request clarification or add additional context in comments.

1 Comment

Glad you found a solution. Maybe consider to set the [Route("/Account")] on the controller scope and then overriding the route on the method scope if necessary to avoid some duplication !
1

Explanation :

  • You logged in on /broker/home so you route is /broker/home/index
  • You then render the menu without specifying a default area (then it is set by default to "broker" when evaluating the tags).
  • The broker area exists so your first route is matched.

So you have the solution to set the asp-area to empty string in your form tag (because the menu can be rendered in an existing area or not)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.