ASP.NET Core adding Area to form post without specifying

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 :

ASP.NET Core adding Area to form post without specifying

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

ASP.NET Core adding Area to form post without specifying

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 😀

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();
        });

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 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)

Method 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 ASP.NET Core adding Area to form post without specifying

 [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>


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x