我正在用会话对象测试一个条件,如果它是假的,我需要重定向到某个地方。我想用它做一个函数。通常我会:
public ActionResult SomeAction()
{
if (Session["level"] == null)
return RedirectToAction("Home", "Whatever");
if ((int)Session["level"] == 1)
return RedirectToAction("Choose", "Whatever");
// The rest of the code
}
但我开始在每一个动作中都有很多…我觉得这是不对的,我想把它们都放在一个函数中,这样我就能集中精力
the rest of the code
是的。
public ActionResult SomeAction()
{
MaybeRedirect();
// The rest of the code
}
public void MaybeRedirect()
{
if (Session["level"] == null)
return RedirectToAction("Home", "Whatever");
if ((int)Session["level"] == 1)
return RedirectToAction("Choose", "Whatever");
}
什么时候?
RedirectToAction
不是由
ActionResult
动作…当然,这没什么用。