【发布时间】:2009-09-17 20:33:32
【问题描述】:
我正在运行 MVC 2 预览版,这是我第一次尝试使用 Ninject2 MVC
我大陆得到的错误是: 尝试创建类型为“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());
}
}
【问题讨论】:
-
一切编译正常,当我启动页面时出现错误。我想我错过了一些非常简单的东西。
-
我只能说,我已经让它在我的 ASP.NET MVC 2 应用程序中工作了......你的代码似乎也没有任何问题。不幸的是,我的代码在家里(目前正在工作),所以我无法引用它。
标签: asp.net-mvc ioc-container ninject