1

I'm using MEF to create a MVC 5 app that support plugins. Using a IControllerFactory and IOC from MEF I've got it working so that /app/specialcontroller/action calls the action and controller from the other dll (plugin).

The problem i have is that when the bundles and content are loaded, the controller factory is failing because content and bundles aren't exported from the plugin.

What I would like to happen is to use the Default factory for certain "controller" names.

Is this possible?

This is the answer I based it on... MEF with MVC 4 or 5 - Pluggable Architecture (2014)

When including app/bundles/jquery I want it to use the MVC controller for bundles

Hope that makes sense, thanks

Community
  • 1
  • 1
sambomartin
  • 6,663
  • 7
  • 40
  • 64

1 Answers1

0

In this scenario I have used the following controller factory in one of my projects earlier -

namespace CaterPiller.WebClient.Factories
{
    public class MvcControllerFactory : DefaultControllerFactory
    {
        private readonly CompositionContainer _container;
        public MvcControllerFactory(CompositionContainer container)
        {
            _container = container;
        }

        protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
        {
            if(controllerType == null)
            {
                return null;
            }
            var export = _container.GetExports(controllerType, null, null).FirstOrDefault();
            return null == export
                       ? base.GetControllerInstance(requestContext, controllerType)
                       : (IController) export.Value;
        }

        public override void ReleaseController(IController controller)
        {
            ((IDisposable) controller).Dispose();
        }
    }
}

for bundles and content, the controller type is usually null, so I just returned null for them -

if(controllerType == null)
                {
                    return null;
                }

and got them served by the default controller. I had my other urls served by controllers only bundles and contents were not, so this worked for me. Probably not the best solution but this solved my problem back then while using MEF for creating controller factory. You can try this solution and this might help you.

brainless coder
  • 6,310
  • 1
  • 20
  • 36
  • My implementation only has CreateController, ReleaseController and GetControllerSessionBehavior. No GetControllerInstance. Am I missing something? – user3036342 May 28 '14 at 14:12
  • GenControllerInstance is the actual resolver for your controller classes (Because with MEF I was using Service Locator Pattern). – brainless coder May 29 '14 at 06:39
  • I understand that, but there is no GetControllerInstance TO override, only CreateController, ReleaseController and GetControllerSessionBehavior – user3036342 May 29 '14 at 07:08
  • @user3036342 Not sure about your implementation, this project of mine is 1.5 year old, so it was probably mvc4, not sure if they changed it on mvc5. – brainless coder May 29 '14 at 10:06