1

I've set up StructureMap as my choice for DI and upon registering my custom controller factory, this error pops up (the error doesn't pop if StructureMap isn't set up). I don't know why this error occurs, it seems oddly buggy, am I missing anything? I've seen comments to this issue around SO but no definitive answer. If there is a correct fix for this that I missed, would you kindly point me in the right direction?

public class StructureMapControllerFactory : DefaultControllerFactory
{
    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
    {
        var appRepo = new ApplicationDbContext();
        var settingsRepo = new SettingRepository(appRepo);

        if (controllerType == typeof(SettingsController))
        {
            return new SettingsController(settingsRepo);
        }

        return base.GetControllerInstance(requestContext, controllerType); 
    }
}

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }

protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);

        // This initializes our database
        Database.SetInitializer<ApplicationDbContext>(new ApplicationDbInitializer());
        ControllerBuilder.Current.SetControllerFactory(new StructureMapControllerFactory());
reZach
  • 8,945
  • 12
  • 51
  • 97
  • possible duplicate of [Custom Controller Factory, Dependency Injection / Structuremap problems with ASP.NET MVC](http://stackoverflow.com/questions/719678/custom-controller-factory-dependency-injection-structuremap-problems-with-asp) – Tieson T. Aug 24 '14 at 05:54

1 Answers1

1

I've just had this perhaps misleading message, when running in debug mode. What has happened for me is that I have removed modernizr from my BundleConfig.cs

//bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
//            "~/Scripts/modernizr-*"));

but when I have created new Areas in my project, it has scaffolded in the call to load modernizr

@Scripts.Render("~/bundles/modernizr")

You need to remove this call to the non-existant bundle. I would suggest it is the same issue that you were having.

I'm sure this information will help somebody in the future.

VictorySaber
  • 3,084
  • 1
  • 27
  • 45
  • 1
    This helped me pinpoint the problem. The previous developers left a mess of spaghetti code and after I cleaned it up I started getting these errors. Turns out they had merged the modernizr bundle with the jquery bundle and forgot to remove the references to it in a few pages. Keep you code clean, kids... – BeanFlicker May 30 '18 at 17:31