MVC 4 - LogOff controller action giving 404 not found

与世无争的帅哥 提交于 2019-12-03 10:34:31

You use a link (<a/> tag) to log off which results in HTTP GET request when user clicks on it, but your action is constrained to serve POST request only (because it is decorated with [HttpPost] attribute).

You either need to put your link into a form and generate POST request, or remove [HttpPost] and [ValidateAntiForgeryToken] (credits to GalacticCowboy) from your action.

Since logout modifies server state, I wouldnt remove [HttpPost] and [ValidateAntiForgeryToken] Instead I will replace the link (anchor tag) with the following

@using (Html.BeginForm("Log Out", "Account", FormMethod.Post,
 new { id = "logoutForm" }))
{

    @Html.AntiForgeryToken()
    <a href="javascript:document.getElementById('logoutForm').submit()">Log Out</a>

}

I ran into this issue on a legacy app. The way that I fixed it was to detect when the supplied return Url was '/Account/LogOff' and act accordingly. From the 'AccountController.cs' file, 'Login' method:

if (returnUrl == "/Account/LogOff")
    {
        return this.RedirectToLocal(null);
    }
    else
    {
        return this.RedirectToLocal(returnUrl);  
    }    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!