0

I am new to MVC world. I have a nav in my Layout file used by logged in users :

<div class="navbar-collapse collapse">
            <ul class="nav navbar-nav">
                <li>@Html.ActionLink("Home", "Index", "Member")</li>
                <li>@Html.ActionLink("My Profile", "MyProfile", "Member")</li>
                <!-- User type Admin, then Admin Menu -->
                <li>@Html.ActionLink("Admin Index", "AdminIndex", "Member")</li>
                <li>@Html.ActionLink("About", "About", "Home")</li>

                <!-- Throws error /User/Logout not found -->
                <li>@Html.ActionLink("Logout", "Logout", "User")</li>

                <!-- Works Perfectly fine, but the link is on new line & different font/color/underlined -->
                @if (Request.IsAuthenticated)
                {
                    using (Html.BeginForm("Logout", "User", FormMethod.Post, new { id = "logoutForm" }))
                    {
                        <li><a href="javascript:document.getElementById('logoutForm').submit()">Logout</a></li>
                    }
                }
            </ul>
        </div>

The @Html.ActionLink("Logout" doesn't work. It throws "/User/Logout" not found Error How to make that work like the below logout with <a href ??? Is the error b'coz in href, their is form and calling Form.Post method ??

Controller code :

public class UserController : Controller
{   
          // Registeration Action
    [HttpGet]
    public ActionResult Registeration() {..... }

    // Login
    [HttpGet]
    public ActionResult Login() {....  }

    // Login Post
    [HttpPost]
    public ActionResult Login(UserLogin userLogin, string returnUrl) { ...}

    // Logout
    [Authorize]
    [HttpPost]
    public ActionResult Logout()
    {
        FormsAuthentication.SignOut();
        return RedirectToAction("Login", "User");
    }
}

Can anyone please help me get this work !! Any help is highly appreciated.

Thanks

Tvd
  • 4,463
  • 18
  • 79
  • 125
  • What error occurs using the `@Html.ActionLink("Logout", "Logout", "User"` link? It would also be helpful if you posted the code from the Logout action in the UserController. Also you can conditionally check if someone is in a role in _layout just fine- I'm guessing you have an error in your role implementation. If your role implementation is error free, you can simple do `if(User.IsInRole("admin") { //show link }` – GregH Jul 10 '17 at 15:21
  • Please refrain from asking multiple questions at the same time. For the first question, please explain why it doesn't work otherwise we can't help you at all. – DavidG Jul 10 '17 at 15:38
  • @DavidG, and peggy Removed 2nd question. And shared error too. – Tvd Jul 10 '17 at 16:06
  • I assume that the code should read `Html.BeginForm("Logout", "Account"...` instead? – DavidG Jul 10 '17 at 16:06
  • @DavidG, the 2nd para action name is "Logout" and controller name is "User". Why "Account" in action name ??? – Tvd Jul 10 '17 at 16:20
  • Is the `Logout` action actually in the `UserController` class? The default MVC template does not work like this. – DavidG Jul 10 '17 at 16:21

1 Answers1

0

I suspect, although it is difficult to tell, that the controller is not UserController but maybe AccountController (the default) or MemberController.

In which case the call should be

 @Html.ActionLink("Logout", "Logout", "Account") 

or

@Html.ActionLink("Logout", "Logout", "Member")

Following your additional edit I think the issue will be that logout is a Post and a link is only ever a Get request so Mvc is looking for a get action /User/logout. There is an answer here about how to convert a link to a Post here.

I would probably get your link to call an ajax command in some js code. To make the most of the Razor goodness you can use @Url.Action. Alter your code as follows.

Your nav will look like this;

<div class="navbar-collapse collapse">
        <ul class="nav navbar-nav">
            <li>@Html.ActionLink("Home", "Index", "Member")</li>
            <li>@Html.ActionLink("My Profile", "MyProfile", "Member")</li>
            <!-- User type Admin, then Admin Menu -->
            <li>@Html.ActionLink("Admin Index", "AdminIndex", "Member")</li>
            <li>@Html.ActionLink("About", "About", "Home")</li>

            <li><a href=# onclick="LogOut()">Log Out</a></li>

                }
            }
        </ul>
</div>

Then your javascript function needs to be somewhere accessible to your nav page (the bottom of your _Layout page is probably easiest).

<script type="text/javascript">
   function LogOut() {
      $.ajax({
           type: "POST",
           url: "@Url.Action("Logout", "User")"
       });
   }
</script>

Hope that makes sense, I haven't tested it but your link should now look like all the others and your controller will now be getting a POST request so will access your logout method. Any problems shout.

Simon Rowe
  • 11
  • 5
  • The action is handled in UserController and not Account or Member Controller. Registration, Login, Logout all such actions are handled in UserController. The layout file - that's a MembersLayout file And used by files of logged in users. Eg. /Member/Index uses MembersLayout which has the Logout issue above. Ps tell what more details should I provide to help you solve the issue. – Tvd Jul 10 '17 at 16:56
  • Can you please elaborate or show the real code in example - as I am poor in js and/or ajax. I see your ajax code, but how will I call it thru my
  • Html.ActionLink("Logout", "Logout", "User")
  • line of code ??? My intention here, is they all look same and in same line. Html.ActionLink & a looks different and href comes in new line. – Tvd Jul 10 '17 at 20:51
  • @Tvd no problem when I get to my machine I will elaborate. – Simon Rowe Jul 11 '17 at 06:42
  • Yes, the link looks like other links; but unfortunately the on click event their no action at all. I have copied your code, making sure their is not typing or so error. But their is no action on the link click event. – Tvd Jul 11 '17 at 18:32
  • @Tvd I think the typo is on me the js command is LogOut() but in the link I have Logout() altered now. – Simon Rowe Jul 12 '17 at 18:31
  • @Tvd did it work for you if so please mark as answered. – Simon Rowe Jul 15 '17 at 11:45
  • No dear, it doesn't work. I had edited the js command at the time of your answer only, but it doesn't perform the action as my a href does. – Tvd Jul 17 '17 at 14:19
  • Ok one last thought...are you using the jQuery library? If not this could explain the issue, I would recommend it generally but certainly for this sort of thing. There is a jsFiddle [here](https://jsfiddle.net/rq10sbx7/) with slightly tweaked code so you can see it in action. After that I am drawing a blank, sorry. – Simon Rowe Jul 17 '17 at 21:23