这是一个 MVC 应用程序,其中控制器在构造函数中需要 aDataContextCreator
和 a CustomerID
。我的ControllerFactory
样子:
public class NinjectControllerFactory : DefaultControllerFactory
{
private IKernel ninjectKernel;
public NinjectControllerFactory()
{
ninjectKernel = new StandardKernel();
AddBindings();
}
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
if (controllerType == null)
{
return null;
}
else
{
string customerID = requestContext.HttpContext.Session["CustomerID"].ToString();
return (IController)ninjectKernel.Get(controllerType, new IParameter[]{new Parameter("CustomerID", customerID, true)});
}
}
private void AddBindings()
{
ninjectKernel.Bind<IDataContextCreator>().To<DefaultDataContextCreator>();
}
}
导航到页面时出现以下错误,即触发控制器的创建:
Ninject.ActivationException: Error activating int
No matching bindings are available, and the type is not self-bindable.
Activation path:
2) Injection of dependency int into parameter CustomerID of constructor of type MyController
1) Request for MyController
以上所有内容都是在 Win 7 上使用 MVC3 .Net 4。感谢您的帮助。