开发者

Castle Windsor with MVC 2.0 and Areas

Wanted to ask a quick question regarding castle windsor and implementing IoC for Controllers in Areas. Does Castle 2.5 support MVC 2.0 areas?

My Castle config works ok for my root controller in the root of my site but any area controllers are not found with a

The IControllerFactory 'XXX.Castle.WindsorControllerFactory' did not return a controller for the name 'Registration'.

I am using Castle directly not through MvcContrib

Code as follows:

class WindsorControllerFactory : DefaultControllerFactory
{
    WindsorContainer container;
    // The constructor:
    // 1. Sets up a new IoC container
    // 2. Registers all components specified in web.config
    // 3. Registers all controller types as components
    public WindsorControllerFactory()
    {
        // Instantiate a container, taking configuration from web.config
        container = new WindsorContainer();

        // Also register all the controller types as transient
        var controllerTypes =
            from t in Assembly.GetExecutingAssembly().GetTypes()
            where typeof(IController).IsAssignableFrom(t)
            select t;
        foreach (Type t in controllerTypes) {
            //container.AddComponentLifeStyle(t.FullName, t, LifestyleType.Transient);
            container.Register(Component.For(t).Named(t.FullName).LifeStyle.Transient);
        }

        container.Install(new WindsorInstaller());
    }

    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
    {
        if (controllerType != null)
   开发者_Python百科     {
            return (IController)container.Resolve(controllerType);
        }

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

Many thanks

Richard


For those who meet this issue in the future I have a solution that fixed my issue. The issue was that my controllers did not have the correct namespace allocated to the directory they were in..

ie I had tsd.Web.Controllers NOT tsd.Areas.Account.Controllers

Setting the namespace path to map the directory structure solved my issue and the castle could then located the controller in the area...!

Regards

Richard


You can return to any Controller, then after you can get exception 404

protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
    {
        if (controllerType != null)
        {
            return (IController)container.Resolve(controllerType);
        }

        return (IController)container.Resolve(typeof(HomeController));
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜