2011-10-03 58 views
1

在ASP.NET MVC 3控制器生命周期中ControllerContext是否构建?在ASP.NET MVC 3控制器生命周期中,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, 班吉

回答

5

When in the ASP.NET MVC 3 Controller lifecycle does the ControllerContext get constructed?

这发生在Initialize方法内。切勿访问控制器构造函数中的任何HttpContext绑定对象。

+0

谢谢 - 这太好了。 – benjy