We recently upgraded one of our clients from Sitecore 8.0 to Sitecore 8.1 and had to upgrade to Web Forms for Marketers 8.1 rev. 160304 as part of the upgrade. After upgrading WFFM, all of our forms started to throw the following exception:

The controller for path '{{form path}}' could not be found.

The project has a custom Controller Factory that uses Castle Windsor to resolve dependencies for controllers. We were using WebActivator and a post-application start method to set the MVC Controller Factory right after application start.

After some digging I found that Sitecore has its own Controller Factory, SitecoreControllerFactory, that decorates the Controller Factory installed into the MVC framework and handles resolution of controllers for Web Forms for Marketers and other Sitecore components. The SitecoreControllerFactory is plugged into the pipeline during the initialize event in the aptly-named InitializeControllerFactory processor. Since our custom Controller Factory was being plugged into the MVC framework post-application start, it was overwriting the SitecoreControllerFactory instead of being decorated by it, which caused our WFFM controllers to no longer resolve.

The exception message we got was specific to the WindsorControllerFactory described here; if you have written your own based on the DefaultControllerFactory provided by MVC, you may get an exception similar to the following when your Controller Factory is plugged into the wrong spot in the Sitecore pipeline:

No parameterless constructor defined for this object

The fix was as simple as moving our Controller Factory initialization into the Sitecore pipeline before the InitializeControllerFactory processor as shown below:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <initialize>
        <processor type="SitecoreDemo.InitializeDependencyResolver, SitecoreDemo"
                   patch:before="processor[@type='Sitecore.Mvc.Pipelines.Loader.InitializeControllerFactory, Sitecore.Mvc']"/>
      </initialize>
    </pipelines>
  </sitecore>
</configuration>

Now our custom Controller Factory gets decorated by the SitecoreControllerFactory instead of overwriting it, allowing for WFFM controllers to get resolved again.