c# - Manually instantiate a Controller in MVC 6 alpha4 -
c# - Manually instantiate a Controller in MVC 6 alpha4 -
i doing experimentation mvc 6 alpha 4. trying activate controller manually , returning instead of homecontroller doesn't work. help please..
so far have created own controller mill code.
public class mycontrollerfactory : icontrollerfactory { public object createcontroller(actioncontext actioncontext) { var actiondescriptor = actioncontext.actiondescriptor microsoft.aspnet.mvc.reflectedactiondescriptor; type controllertype = type.gettype("hello.controllers.mycontroller"); var controller = _typeactivator.createinstance(_serviceprovider, controllertype); actioncontext.controller = controller; _controlleractivator.activate(controller, actioncontext); homecoming controller; } }
i have debugged code. constructor of mycontroller
gets called , mycontroller
beingness returned createcontroller
method error. debugger never reaches iactionresult index()
. here error get.
system.reflection.targetexception: object not match target type. @ system.reflection.runtimemethodinfo.checkconsistency(object target) @ system.reflection.runtimemethodinfo.invokeargumentscheck(object obj, bindingflags invokeattr, binder binder, object[] parameters, cultureinfo culture) @ system.reflection.runtimemethodinfo.invoke(object obj, bindingflags invokeattr, binder binder, object[] parameters, cultureinfo culture) @ system.reflection.methodbase.invoke(object obj, object[] parameters) @ microsoft.aspnet.mvc.reflectedactionexecutor.<executeasync>d__2.movenext()
here answer.
firstly, mycontroller
needs inherit homecontroller
. secondly, need alter 'controller' value in routedata before activating controller.
actioncontext.routedata.values["controller"] = "my";
c# asp.net-mvc asp.net-core-mvc
Comments
Post a Comment