MVC 2 Beta DefaultControllerFactory with Areas

Posted by stoto on Stack Overflow See other posts from Stack Overflow or by stoto
Published on 2009-11-25T18:59:29Z Indexed on 2010/05/18 5:50 UTC
Read the original article Hit count: 222

Why default factory WON'T return full name of the controllers (with namespaces)? I'm using Service Locator and autofac.

using System.Web.Mvc;

using Microsoft.Practices.ServiceLocation;

namespace Application.Core.MVC
{

        public override IController CreateController(System.Web.Routing.RequestContext requestContext, string **controllerName**)
        {
            return ServiceLocator.Current.GetInstance<IController>(controllerName);
        }
}

I had two home controllers (one under area Blog)

http://localhost/Home

http://localhost/Blog/Home

controllerName return only "Home" without full qualified name for both in above code. This creates a problem when I try to regiser controllers' names for dependency injection. Here is how I register controllers right now according to this situation. Even this brings up the pages without exception. But When I access http://localhost/Home, both controllers invoked regardlessly.

   foreach (var tp in currentAssemblyControllersTypes)
                    builder.Register(tp).FactoryScoped().Named(tp.Name.Replace("Controller", ""));

Anyone can help?Thanks.

© Stack Overflow or respective owner

Related posts about mvc

Related posts about inversion-of-control