注销按钮在MVC应用程序中不起作用 [英] Log out button not working in mvc application

查看:94
本文介绍了注销按钮在MVC应用程序中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有不同用户帐户和密码的MVC应用程序.登录功能工作正常,但是当我单击注销按钮时,出现404错误,提示:

I have an MVC application with different user accounts and passwords. The log in feature works fine, but when I click the log out button I get a 404 error saying:

"/"应用程序中的服务器错误.

Server Error in '/' Application.

找不到资源. 说明:HTTP404.您正在寻找的资源(或其依赖项之一)可能已被删除,名称已更改,或者暂时不可用.请检查以下网址,并确保其拼写正确>正确.

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中的代码

This is the code in the AccountController

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult LogOff()
    {
        Session.Clear();
        FormsAuthentication.SignOut();
        return RedirectToAction("Index", "Home");
    }

推荐答案

Html.ActionLink呈现常规超链接,这会导致GET请求.

Html.ActionLink renders a regular hyperlink, which causes a GET request.

您的LogOff具有HttpPost属性,这意味着它将仅回答POST请求.删除该属性,您的操作将起作用. ValidateAntiForgeryToken也不是必需的,它用于表单.

Your LogOff has the HttpPost attribute, which means it'll only answer to POST requests. Remove the attribute and your action will work. The ValidateAntiForgeryToken is also not needed, it is for forms.

注意:我不再支持我的原始答案.尽管它解决了当前的问题,但由于浏览器预取

Note: I no longer stand by my original answer. Although it fixes the problem at hand, logging out should really be a POST instead, due to browser prefetching and potential malicious behaviour. So instead of using a hyperlink, there should be a form with a submit button styled as a hyperlink.

这篇关于注销按钮在MVC应用程序中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆