我运行一个MVC 2预览,这是我第一次尝试使用Ninject2 MVCNinject MVC是否应该与MVC 2 Preview一起使用?
有错误,我continently得到的是: 试图创建类型“MyMVC.Controllers的控制器时发生错误。 EventsController”。确保控制器有一个无参数的公共构造函数。
我在我的Global.cs是这样的:
public class MvcApplication : NinjectHttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("elmah.axd");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
routes.MapRoute(
"Root",
"",
new { controller = "Home", action = "Index", id = "" }
);
}
protected override void OnApplicationStarted()
{
RegisterRoutes(RouteTable.Routes);
RegisterAllControllersIn(Assembly.GetExecutingAssembly());
}
protected override IKernel CreateKernel()
{
return new StandardKernel(new ServiceModule());
}
}
internal class ServiceModule : NinjectModule
{
public override void Load()
{
Bind<IEventService>().To<EventService>();
Bind<IEventRepository>().To<EventRepository>();
}
}
而这正是我的控制器的样子。
public class EventsController : Controller
{
private IEventService _eventService;
//
// GET: /Events/
public EventsController(IEventService eventService)
{
_eventService = eventService;
}
public ActionResult Index(string name)
{
return View(_eventService.GetEvent(name));
}
public ActionResult UpcomingEvents()
{
return View(_eventService.GetUpcomingEvents().Take(3).ToList());
}
}
一切都很好编译,当我启动页面时,当我到达那里的错误。我想我错过了很简单的事情。 – OneSmartGuy 2009-09-17 20:34:59
我只能说我已经在我的ASP.NET MVC 2应用程序中工作了......你的代码似乎没有任何问题。不幸的是,我的代码在家里(目前工作),所以我不能引用它。 – Charlino 2009-09-17 21:33:26