我運行一個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