c# - Load Controllers at runtime from another assembly -


in project in main web assembly before register routes in global.asax load number of external assemblies. of these assemblies have mvc controllers , views inside of them.

my problem these external controllers not picked reason, assume controller registration happens somewhere before assemblies loaded, because if reference assemblies before application starts(dlls in bin folder) works fine.

so i'm wondering if there`s

  • a way control when mvc controllers registered, can after assemblies loaded? or
  • a way inject external controllers?

i had similar issue webapi controllers before , able fix using custom httpcontrollerselector, wasn't able find similar in mvc yet.

was able solve issue overriding defaultcontrollerfactory's createcontroller method:

application_start:

icontrollerfactory factory = new controllerfactory(); controllerbuilder.current.setcontrollerfactory(factory); 

controllerfactory:

public class controllerfactory : defaultcontrollerfactory {     public override icontroller createcontroller(requestcontext requestcontext, string controllername)     {          icontroller controller = null;          type controllertype;          if (controllertypes.trygetvalue(controllername.tolower(), out controllertype))          {               controller = (icontroller) activator.createinstance(controllertype);          }          return controller;     }     ... } 

controllertypes dictionary of mvc controllers external assemblies.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -