注销按钮不起作用MVC

时间:2014-05-08 16:21:30

标签: asp.net-mvc

我有一个logoutbutton看起来像

<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

4 个答案:

答案 0 :(得分:0)

设置按钮ID并使用javascript捕获它。你可以使用委托$(document).delegate(&#39;#btnid&#39;,&#39;点击&#39;,function(){location.href =&#39; controller / methodname&#39 ;;} );尝试这样的事情。并确保在加载页面加载的脚本中添加了某种脚本。

答案 1 :(得分:0)

您的操作具有[HttpPost]属性,您的JS代码正在发出GET请求。尝试删除该属性,或者更好地让你的js进行POST。

答案 2 :(得分:0)

两个问题:

您不应将“控制器”一词包含在路径中:

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

同时从您的操作中删除HttpPost属性,因为您希望允许从GET请求注销

答案 3 :(得分:0)

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