在ASP.NET MVC 3 Controller生命周期中是否构建了controllerContext?

时间:2011-10-03 19:03:47

标签: c# asp.net-mvc-3

当在ASP.NET MVC 3 Controller生命周期中构建ControllerContext时?

我在控制器的构造函数中有以下代码:

if ((Session == null || Session.IsNewSession || Session["SecureAuthHash"] == null) &&
    (ValueProvider == null || ValueProvider.GetValue("controller").ToString() != "Account"))
{
    RedirectToAction("Login", "Account");
}

我收到以下错误:

Exception Details: System.ArgumentNullException: Value cannot be null.
Parameter name: controllerContext

controllerContext何时可用,以便我的代码可以运行?这应该在任何其他代码之前运行,所以我真的不想把它放在每个动作方法中...但我想这可能是唯一的方法。

TIA, 本吉

1 个答案:

答案 0 :(得分:5)

  

当在ASP.NET MVC 3 Controller生命周期中构建ControllerContext时?

这发生在Initialize方法中。永远不要访问控制器构造函数中的任何HttpContext绑定对象。