Why is my Castle Windsor controller factory's GetControllerInstance() being called with a null value?

后端 未结 4 803
遥遥无期
遥遥无期 2021-02-04 10:30

I am using Castle Windsor to manage controller instances (among other things). My controller factory looks like this:

public class WindsorControllerFactory : Def         


        
相关标签:
4条回答
  • 2021-02-04 11:08

    regarding registration of all the controllers you'd usually do it like this:

    container.Register(
       AllTypes.FromThisAssembly()
          .BasedOn<IController>()
          .Configure(c => c.Lifestyle.Transient)
    );
    

    See the documentation for more explanation of the API.

    0 讨论(0)
  • 2021-02-04 11:16

    Had this problem when following the Pro ASP.NET MVC Framework book, added

    routes.IgnoreRoute("favicon.ico");

    to the routes in the global.asax.cs file and it works. See more here: serving favicon.

    0 讨论(0)
  • 2021-02-04 11:20

    Very late addition: The step-by-step tutorial at the windsor site seems good. It breaks down the creation of a controller and how it's loaded into the factory. It also covers the "favicon.ico" noise.

    http://docs.castleproject.org/(S(0jvahybwt45sgwzwirpa3455))/Windsor.Windsor-tutorial-part-one-getting-Windsor.ashx

    0 讨论(0)
  • 2021-02-04 11:25

    It turns out that the second request was the MVC framework trying to find a script I included in the Site.Master. The path did not exist, so I guess it tried to resolve a controller (that matched /Scripts/sitescripts.js). I changed the method to this:

    protected override IController GetControllerInstance(Type controllerType)
    {
        if (controllerType != null)
        {
           return (IController)_container.Resolve(controllerType);
        }
        else
        {
           return base.GetControllerInstance(controllerType);
        }
    }
    

    And an exception with an understandable message was thrown.

    0 讨论(0)
提交回复
热议问题