注销按钮不起作用 MVC



我有一个注销按钮看起来像

<article class="PanelTextRight">
    Hello, @Html.ActionLink(User.Identity.Name, "Manage", "Account", routeValues: null, htmlAttributes: new { @class = "username", title = "Manage" })!
    <article class="LogBtn">
        <input type="submit"   id="btnLogOut"  value="Log Off">
    </article>
</article>

然后我添加这个脚本

<script>
        $(document).delegate('#btnLogOut', 'click', function () { location.href = 'AccountController/LogOff'; });
    </script>

所有的工作,除了注销按钮,任何想法如何修复它,我有一个注销操作看起来像

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult LogOff()
{
    WebSecurity.Logout();
    FormsAuthentication.SetAuthCookie("username", false);
    FormsAuthentication.SignOut();
    return RedirectToAction("Index", "Home");
}

如何修复这个按钮,它给出错误

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: /AccountController/LogOff

设置按钮id并使用javascript捕获它。你可以使用delegate $(document).delegate('#btnid', 'click',function(){location。Href = 'controller/methodname';});试试这样做。确保这种脚本被添加到加载页面的脚本中。

你的动作有一个[HttpPost]属性,你的JS代码正在做一个GET请求。试着删除这个属性,或者让你的js做一个POST。

两个问题:

你不应该把"Controller"这个词作为你的路径的一部分:

$(document).delegate('#btnLogOut', 'click', function () { location.href = 'Account/LogOff'; });

也从你的动作中删除HttpPost属性,因为你想允许从GET请求注销

您的操作是HttpPOST方法,将其更改为HttpGET,并删除ValidateAntiForgeryToken属性。

最新更新