Now run project and show error on line return controllerType == null
? null
: (IController)ninjectKernel.Get(controllerType);
Error activating HomeController using implicit self-binding of HomeController No constructor was available to create an instance of the implementation type.
Activation path: 1) Request for HomeController
Suggestions: 1) Ensure that the implementation type has a public constructor. 2) If you have implemented the Singleton pattern, use a binding with InSingletonScope() instead.
I think
I knowwhat the problem.
My project isloaded by default
from thehomecontroller / index.
But allrespect todisplay a list ofitemsI implemented
inProductController.MaybeI'm wrong,if not,
how to solve thisproblem.
public class ProductController : Controller
{
IProductsRepository repository;
public int pageSize = 4;
public ProductController(IProductsRepository productRepository)
{
repository = productRepository;
}
public ViewResult List(int page = 1)
{
ProductsListViewModel viewModel = new ProductsListViewModel
{
Products = repository.Products.
OrderBy(p => p.IdProduct).
Skip((page - 1) * pageSize).
Take(pageSize),
PagingInfo = new PagingInfo
{
CurrentPage = page,
ItemsPerPage = pageSize,
TotalItems = repository.Products.Count()
}
};
return View(viewModel);
}
}
public class NinjectControllerFactory : DefaultControllerFactory
{
private IKernel ninjectKernel;
public NinjectControllerFactory()
{
ninjectKernel = new StandardKernel();
AddBindings();
}
protected override IController GetControllerInstance(RequestContext requestContext,
Type controllerType)
{
return controllerType == null
? null
: (IController)ninjectKernel.Get(controllerType);
}
private void AddBindings()
{
ninjectKernel.Bind<IProductsRepository>().To<EFProductRepository>();
}
}
public class HomeController : Controller
{
HaengemattenEF myShop = new HaengemattenEF();
public HomeController(IProductsRepository productRepository)
{
}
public ActionResult Index()
{
//slide images data
var list = from n in myShop.SlideImages
where n.IdSlideImg != null
select n;
ViewBag.ImgList = list.ToList<SlideImages>();
//scroll images data
var scrollList = from m in myShop.ScrollImages
where m.IdImgScroll != null
select m;
ViewBag.Scroll = scrollList.ToList<ScrollImages>();
return View();
}
public ActionResult About()
{
return View();
}
}
namespace haengematten.eu.Models
{
public class EFProductRepository: IProductsRepository
{
private HaengemattenEF context = new HaengemattenEF();
public IQueryable<Products> Products { get { return context.Products; } }
}
}
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "HomeController", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory());
}
}
I don't see any evident error. Everything seams fine...Try giving full thrust to your application in VS 2010 web config to see if there is some problem of permissions.
DimiBy
Member
187 Points
233 Posts
Not found in the method GetControllerInstance parammetr Type controllerType
Apr 12, 2012 10:58 AM|LINK
Hello
Not found in the method GetControllerInstance parammetr Type controllerType.Cant undestand, why. How solve this problem?
public class NinjectControllerFactory: DefaultControllerFactory { private IKernel ninjectKernel; public NinjectControllerFactory() { ninjectKernel = new StandardKernel(); AddBindings(); } private void AddBindings() { ninjectKernel.Bind<IProductsRepository>().To<EFProductRepository>(); } protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType) { return controllerType == null ? null : (IController)ninjectKernel.Get(controllerType); } }raduenuca
All-Star
24675 Points
4250 Posts
Re: Not found in the method GetControllerInstance parammetr Type controllerType
Apr 12, 2012 11:04 AM|LINK
you're saying that the GetControllerInstance method from the DefaultControllerFactory class does not contain a controllerType paramter?
Make sure that DefaultControllerFactory is from System.Web.Mvc namespace or explain the problem in more details
Radu Enuca | Blog
francesco ab...
All-Star
20912 Points
3279 Posts
Re: Not found in the method GetControllerInstance parammetr Type controllerType
Apr 12, 2012 11:10 AM|LINK
probably you have not registred the controller factory in the global.asax:
ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory());
Mvc Controls Toolkit | Data Moving Plug-in Videos
DimiBy
Member
187 Points
233 Posts
Re: Not found in the method GetControllerInstance parammetr Type controllerType
Apr 12, 2012 11:37 AM|LINK
I have this
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory());
}
DimiBy
Member
187 Points
233 Posts
Re: Not found in the method GetControllerInstance parammetr Type controllerType
Apr 12, 2012 11:54 AM|LINK
Now run project and show error on line return controllerType == null
? null
: (IController)ninjectKernel.Get(controllerType);
Error activating HomeController using implicit self-binding of HomeController
No constructor was available to create an instance of the implementation type.
Activation path:
1) Request for HomeController
Suggestions:
1) Ensure that the implementation type has a public constructor.
2) If you have implemented the Singleton pattern, use a binding with InSingletonScope() instead.
DimiBy
Member
187 Points
233 Posts
Re: Not found in the method GetControllerInstance parammetr Type controllerType
Apr 12, 2012 12:06 PM|LINK
I think I know what the problem. My project is loaded by default from the homecontroller / index. But all respect to display a list of items I implemented in ProductController. Maybe I'm wrong, if not, how to solve this problem.
DimiBy
Member
187 Points
233 Posts
Re: Not found in the method GetControllerInstance parammetr Type controllerType
Apr 12, 2012 12:27 PM|LINK
Now show error Method not found: 'System.Delegate System.Reflection.MethodInfo.CreateDelegate(System.Type)'.
Cant undestand why. Please, Help.
francesco ab...
All-Star
20912 Points
3279 Posts
Re: Not found in the method GetControllerInstance parammetr Type controllerType
Apr 12, 2012 01:17 PM|LINK
There is something missing for sure. Probably the pronlem is with the constructor as suggested by the error message you receive.
Your controller must have either a constructor with no parameters, or a constructor with parameters whose types have been mapped with Ninject.
Typically you put an IRepository in the constructor of your controller, and then you instance it with an actual Repository with Minject.
Wgere have you done this?
Mvc Controls Toolkit | Data Moving Plug-in Videos
DimiBy
Member
187 Points
233 Posts
Re: Not found in the method GetControllerInstance parammetr Type controllerType
Apr 12, 2012 06:45 PM|LINK
public class ProductController : Controller { IProductsRepository repository; public int pageSize = 4; public ProductController(IProductsRepository productRepository) { repository = productRepository; } public ViewResult List(int page = 1) { ProductsListViewModel viewModel = new ProductsListViewModel { Products = repository.Products. OrderBy(p => p.IdProduct). Skip((page - 1) * pageSize). Take(pageSize), PagingInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = pageSize, TotalItems = repository.Products.Count() } }; return View(viewModel); } }public class NinjectControllerFactory : DefaultControllerFactory { private IKernel ninjectKernel; public NinjectControllerFactory() { ninjectKernel = new StandardKernel(); AddBindings(); } protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType) { return controllerType == null ? null : (IController)ninjectKernel.Get(controllerType); } private void AddBindings() { ninjectKernel.Bind<IProductsRepository>().To<EFProductRepository>(); } }public class HomeController : Controller { HaengemattenEF myShop = new HaengemattenEF(); public HomeController(IProductsRepository productRepository) { } public ActionResult Index() { //slide images data var list = from n in myShop.SlideImages where n.IdSlideImg != null select n; ViewBag.ImgList = list.ToList<SlideImages>(); //scroll images data var scrollList = from m in myShop.ScrollImages where m.IdImgScroll != null select m; ViewBag.Scroll = scrollList.ToList<ScrollImages>(); return View(); } public ActionResult About() { return View(); } }namespace haengematten.eu.Models { public class EFProductRepository: IProductsRepository { private HaengemattenEF context = new HaengemattenEF(); public IQueryable<Products> Products { get { return context.Products; } } } }public class MvcApplication : System.Web.HttpApplication { public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErrorAttribute()); } public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "HomeController", action = "Index", id = UrlParameter.Optional } // Parameter defaults ); } protected void Application_Start() { AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory()); } }francesco ab...
All-Star
20912 Points
3279 Posts
Re: Not found in the method GetControllerInstance parammetr Type controllerType
Apr 12, 2012 07:56 PM|LINK
I don't see any evident error. Everything seams fine...Try giving full thrust to your application in VS 2010 web config to see if there is some problem of permissions.
Mvc Controls Toolkit | Data Moving Plug-in Videos