开发者

few bugs in pro asp.net mvc 3 book from aPress

I'm reading this book and I no matter what I try I cannot make this example run properly as it uses ninject I have never even heard of before I would appreciate if someone could help me get it running

Author(s) of this book says that I should add new folder (infrastructure) and add new .cs file (NinjectControllerFactory) to project and then add following code to it

private IKernel ninjectKernel;
public NinjectControllerFactory()
{
    ninjectKernel = new StandardKernel();
    AddBindings();
}

//problem with override
protected override IController GetC开发者_如何转开发ontrollerInstance(RequestContext requestContext, Type controllerType)
{
    return controllerType == null
        ? null
        : (IController) ninjectKernel.Get(controllerType);
}

If I try to debug it I have a problem as VS says that I cannot override that method. I have simply removed override and tried to run it again but once again I have a problem with this line of code:

ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory());

I had to add it global.aspx -> Application_Start() method but I have a problem as VS says that I have to cast it to IControllerFactory so i have modified it to

ControllerBuilder.Current
    .SetControllerFactory(new NinjectControllerFactory() as IControllerFactory);

but now I get ArgumentNullException on that line.

Anyone know how can I fix this problem?


You either want to

  1. inherit your class from System.Web.Mvc.DefaultControllerFactory which includes GetControllerInstance to override
  2. get hold of ninject.web.mvc which already implements NinjectControllerFactory for you
    (although it looks like they use a different mechanism for MVC3?)
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜