我有一个具有不同用户帐户和密码的 MVC 应用程序。登录功能工作正常,但是当我单击注销按钮时,出现 404 错误:
Server Error in '/' Application.
The resource cannot be found. Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had >its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled >correctly.
Requested URL: /Account/LogOff
这是注销按钮的代码:
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>@Html.ActionLink("Log Off", "LogOff", "Account")</li>
</ul>
</div>
这是AccountController中的代码
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult LogOff()
{
Session.Clear();
FormsAuthentication.SignOut();
return RedirectToAction("Index", "Home");
}
最佳答案
Html.ActionLink
呈现常规超链接,这会引发 GET 请求。
您的LogOff
具有HttpPost
属性,这意味着它只会响应POST 请求。删除该属性,您的操作将起作用。 ValidateAntiForgeryToken
也不是必需的,它用于表单。
注意:我不再坚持我原来的答案。虽然它解决了手头的问题,但由于 browser prefetching,注销实际上应该是一个 POST和 potential malicious behaviour .因此,不应使用超链接,而应该有一个带有样式为超链接的提交按钮的表单。
关于c# - 注销按钮在 mvc 应用程序中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24208871/