When in the ASP.NET MVC 3 Controller lifecycle does the ControllerContext get constructed?
I have the following code in my controller’s constructor:
if ((Session == null || Session.IsNewSession || Session["SecureAuthHash"] == null) &&
(ValueProvider == null || ValueProvider.GetValue("controller").ToString() != "Account"))
{
RedirectToAction("Login", "Account");
}
And I get the following error:
Exception Details: System.ArgumentNullException: Value cannot be null.
Parameter name: controllerContext
When is the controllerContext available so my code can run? This is supposed to run before any other code, so I really don’t want to have to put it in each action method…but I guess that might be the only way.
TIA,
Benjy
This happens inside the
Initializemethod. Never access any HttpContext bound object in a controller constructor.