This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
Unable to set ViewBag
or ViewData
entries from Controller
constructors
#1422
Labels
The following works in MVC 5.2 but throws an
InvalidOperationException
in vNext. This prevents common initialization scenarios unless the user is aware ofOnActionExecuting()
orOnActionExecutionAsync()
(which are less discoverable than MVC 5.2'sInitialize()
method).ViewData["Items"]
doesn't help becauseViewData
isnull
.Details:
The root cause is
DefaultControllerActivator
creates the controller'sViewDataDictionary
instance. SoViewData
isnull
untilController
instances are activated. Worse ifViewBag
is used whileViewData
is stillnull
, the result is anInvalidOperationException
thrown from theDynamicViewData.ViewData
getter.By contrast, few properties in the MVC 5.2
Controller
class even returnnull
(let alone throw) if invoked from the constructor. (Exceptions includeControllerContext
,Url
, and anythingControllerContext
-derived e.g.HttpContext
andUser
. All of those simply returnnull
prior to theInitialize()
call.)Probably need to revisit the
Controller
activation model. At least should look at the properties lazy-initialized in MVC 5.2 and decide if they should be handled similarly in vNext.The text was updated successfully, but these errors were encountered: