Structuremap does not work on MVC4

try adding this class as your ControllerFactory, I've actually seen the error above in MVC3 and this usually fixed it for me

public class StructureMapControllerFactory : DefaultControllerFactory
{
    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
    {
        try
        {
            return (controllerType == null)
                       ? base.GetControllerInstance(requestContext, controllerType)
                       : ObjectFactory.GetInstance(controllerType) as IController;
        }
        catch (Exception ex)
        {
            return null;
        }
    }
}

oops.I found an emergency solution :) Try to implement a class from IControllerActivator

public class StructureMapControllerActivator : IControllerActivator
{
   private IContainer _container;
    
   public StructureMapControllerActivator(IContainer container)
   {
       _container = container;
   }
    
   public IController Create(RequestContext requestContext, Type controllerType)
   {
       return _container.GetInstance(controllerType) as IController;
   }
}

and then register it to the IoC class:

 x.For<IControllerActivator>().Use<StructureMapControllerActivator>();

and then enjoy it. Good luck


If you remove any old configuration for structuremap and install structurmap.mvc4 from nuget then configure your IoC container, you don't have any problems.