找不到资源 asp.net mvc

Resource could not be found asp.net mvc

我正在熟悉 asp.net mvc、Identity 并尝试简单注销但得到 The resource cannot be found.

在我的 html 页面中

<li><a href="@Url.Action("LogOff", "Account")" id="home">Log off</a></li>

这是在 AccountController

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

这在我的路由配置文件中

 routes.MapRoute(
         null,
         "LogOff",
         new { controller = "Account", action = "LogOff" }

我可能遗漏了什么?

LogOff 方法中删除 [HttpPost]

[ValidateAntiForgeryToken]
public ActionResult LogOff()
{
    AuthenticationManager.SignOut();
    return RedirectToAction("Index", "Home");
}

简单地说就是去掉 HttpPost

public ActionResult LogOff()
{
    AuthenticationManager.SignOut();
    return RedirectToAction("Index", "Home");
}

适合我

使用post方式注销。请参阅以下示例。

   @using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm" })) {
            @Html.AntiForgeryToken()
            <a href="javascript:document.getElementById('logoutForm').submit()">Log off</a>
        }

默认情况下,当您使用某些身份验证创建项目时,它会创建具有 HttpPost 属性的 LogOff 方法。

所以你有两种方法来修复它

第一个一个是去掉这个属性。让你的代码看起来像

[ValidateAntiForgeryToken]
public ActionResult LogOff()
{
    AuthenticationManager.SignOut();
    return RedirectToAction("Index", "Home");
}

第二个 一个是将您的 html 更改为使用 @Html.BeginForm 和 POST 方法 docs here

默认项目使用第二种实现方式

编辑 当我输入这个答案时 Rad 给出一个示例如何使用 BeginForm 助手