I built my own Controller class which inherits from BaseController. However the ControllerContext in constructor is “null”. Where should I specify the ControllerContext?
I built my own Controller class which inherits from BaseController . However the ControllerContext
Share
The
ControllerContextproperty is not assigned to in any of the base constructors in your inheritance hierachy. A controller is created by a controller factory and passed back without theControllerContextproperty being assigned to.Using Reflector, we can look at where the assignment takes place:
The
Initializemethod is invoked from the virtual Execute method call:This means the earliest point at which you can access the
ControllerContextproperty is by overriding theExecuteorInitializemethod (but callingbase.Executeorbase.Initializefirst):The latter (
Initialize) is the absolute earliest point at which you can use the ControllerContext property, unless you handled the assignment yourself, which is not recommended (as parts of the framework will be dependent on having that property assigned to at that time).Hope that helps.