asp.net-mvcrazoractionresultredirecttoaction

Problem with RedirectoAction in Layout for logging out


I have an ActionLink in Shared layout like this

@Html.ActionLink("Logout", "Logout", "Home", new { @class = "navbar-btn btn btn-login" })

and in the controller of Home

public ActionResult Logout()
    {
        Session.Abandon();
        Response.Cookies.Clear();

        return RedirectToAction("Login","Home");
    }

Then when I press the Logout button, if the web page is within /Home, it return to the /Home/Login properly, with all session is cleared. But problem occur when I try to press Logout outside of /Home, e.g. in /CourseUser, the URL return then is /CourseUser/Logout?Length=4and it doesnt exist

What I wanted is to return the URL '/Home/Logout' under all circumstance when I press 'Logout'


Solution

  • Kudos to https://www.c-sharpcorner.com/UploadFile/abhikumarvatsa/ajax-actionlink-and-html-actionlink-in-mvc/

    The problem came from

    @Html.ActionLink("Logout", "Logout", "Home", new { @class = "navbar-btn btn btn-login" })
    

    The Home in this case is route value, not controller name. Solution:

    @Html.ActionLink("Đăng xuất", "Logout", "Home", null, new { @class = "navbar-btn btn btn-login", @type = "button" })