简体   繁体   English

找不到资源asp.net MVC

[英]Resource could not be found asp.net mvc

I am familiarizing myself with asp.net mvc, Identity and trying a simple log off but get The resource cannot be found. 我使自己熟悉了asp.net mvc,Identity,并尝试了一个简单的注销,但获取The resource cannot be found.

in my html page 在我的html页面中

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

and this is in the AccountController 这是在AccountController中

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

and this in my route config file 这在我的路线配置文件中

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

What could I be missing? 我可能会缺少什么?

Remove [HttpPost] from LogOff method LogOff方法中删除[HttpPost]

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

simply putting by removing HttpPost 简单地通过删除HttpPost

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

works for me 为我工作

Use the post method to log off. 使用post方法注销。 See the below example. 请参见以下示例。

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

By default when you creating project with some Authentication it creates LogOff method with HttpPost attribute. 默认情况下,当您使用某些身份验证创建项目时,它将使用HttpPost属性创建LogOff方法。

So You have two ways to fix it 所以你有两种方法可以解决它

first one is to remove this attribute. 一个是删除此属性。 And let your Code looks like 并让您的代码看起来像

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

second one is to change your html to use @Html.BeginForm with POST method docs here . 第二个方法是将您的html更改为将@Html.BeginForm与POST方法docs一起使用。

By default projects used second type of realization 默认情况下,项目使用第二种类型的实现

EDIT While I was typing this answer Rad give an sample how to use BeginForm helper here it is 编辑当我输入此答案时, Rad给出了一个示例, 在这里它是如何使用BeginForm助手的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM