【问题标题】:404 on RedirectToAction("Logoff", "Account")404 on RedirectToAction(“注销”,“帐户”)
【发布时间】:2018-11-22 05:50:44
【问题描述】:

当我删除一个用户时,我希望它自动注销,但每当我使用 RedirectToAction 调用注销函数时,我都会收到 404。我在 stackoverflow 的其他地方看到,这是试图通过按钮单击并且 [HttpPost] 和 [HttpGet] 的方法发生冲突 - 但这里似乎不是这种情况。

用户控制器

[HttpPost]
public ActionResult DeleteConfirmed(int id)
{
    User user = db.Users.Find(id);
    db.Users.Remove(user);
    db.SaveChanges();
    return RedirectToAction("LogOff", "Account");
}

账户控制者

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

有人知道为什么会这样吗?网址看起来不错。

【问题讨论】:

  • 当您 RedirectToAction 浏览器发出 Get 请求时,您的控制器操作是 Post,因此找不到。据我所知,您不能重定向到 Post 动词。
  • @JonathonChase 这似乎是一个奇怪的问题,但是否可以将 LogOff 函数定义为 GetRequest ......如果是这样。你会建议有一个单独的注销功能吗?
  • 我根本不知道您需要重定向到注销方法。如果您的注销过程以某种方式变得复杂,我会将其推送到它自己的类中,并从需要注销的控制器中调用它。
  • 你告诉我的那一点点就足以让我找到解决方案。谢谢:)

标签: c# asp.net-mvc


【解决方案1】:

can't redirect to a POST 行动。 一种可能是排除您的注销操作并调用它:

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

或者只需添加您要保存的一行:

[HttpPost]
public ActionResult DeleteConfirmed(int id)
{
    User user = db.Users.Find(id);
    db.Users.Remove(user);
    db.SaveChanges();
    AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
    return RedirectToAction("Index", "Home");
}

【讨论】:

  • 感谢您的回答,但我在 AutenticationManager 中没有 SignOut 方法
  • 但是我从您的原始帖子中复制了确切的代码...?
  • 哦,你是对的!我不知道那里发生了什么。我再试一次XD
  • 啊,我明白了。我判断的有点快。 AuthenticationManager 是在 AccountController 中声明且不在 UserController 中的接口 - 因此它在 UserController 中不可用。因此,对于使用 LogOffAction 函数的第一个解决方案,我假设您希望我将其声明为 HTTPGet 并通过 RedirectToAction 调用它?似乎将注销功能作为 Get 是“危险的”,请参见此处:stackoverflow.com/questions/25710805/…
  • 我只是将它展示为一种可重复使用的分解方法,而不是 GET。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-26
  • 1970-01-01
  • 1970-01-01
  • 2019-09-12
  • 2017-05-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多