Scala - 不能使用返回 play.api.mvc.Result 的方法作为请求的处理程序

Scala - Cannot use a method returning play.api.mvc.Result as a Handler for requests

我在 Scala 中有这个控制器:

def commonRedirect(anId: Long) = {
implicit val aRule = CommonClient.getTheRule(anId)
aRule match {
  case false ⇒ Redirect("/general-rule/" + anId)
  case true  ⇒ Redirect("/custom-rule/" + anId)
}

}

但是,这会导致错误:"Cannot use a method returning play.api.mvc.Result as a Handler for requests"。

如果我应用 Action Builder,它会起作用,但这不是我想要的方式。

有解决此问题的想法吗?

谢谢。

你需要做一个Action

def commonRedirect(anId: Long) = Action {
  implicit val aRule = CommonClient.getTheRule(anId)
  aRule match {
    case false ⇒ Redirect("/general-rule/" + anId)
    case true  ⇒ Redirect("/custom-rule/" + anId)
  }
}