1

我有一个工作的 Visual Studio 项目,我想用 Mono 和 MonoDevelop 运行 Mac。该项目是一个带有 Ninject MVC 的 ASP.NET MVC 3 应用程序,它基本上在控制器上注入了一些接口实现。

将所有 ASP.NET MVC dll 和 Ninject 依赖项添加到项目中后,编译成功。但是当我去运行它时,我遇到了错误:

未找到类型 WebActivatorTest.Controllers.HomeController 的默认构造函数。

我的控制器有以下代码:

public class HomeController : Controller
{
    INotifier _notifier;
    public HomeController(INotifier notifier_)
    {
        _notifier = notifier_;
    }

    public ActionResult Index()
    {
        ViewBag.Name = _notifier.Person();

        return View();
    }

}

我不想有一个空的构造函数,因为我现在有一个 AppStart 代码注册我的接口:

private static void RegisterServices(IKernel kernel)
{
     kernel.Bind<WebActivatorTest.Models.INotifier>().To<WebActivatorTest.Models.Notifier>();
}

此代码在 Windows/Visual Studio 上完美运行,但在 Mono 上无法运行。有人可以帮我吗?

完整的错误是:

Server Error in '/' Application

Default constructor not found for type WebActivatorTest.Controllers.HomeController.

Description: HTTP 500. Error processing request.

Stack Trace:

System.MissingMethodException: Default constructor not found for type     WebActivatorTest.Controllers.HomeController.
at System.Activator.CreateInstance (System.Type type, Boolean nonPublic) [0x00000] in <filename unknown>:0 
at System.Activator.CreateInstance (System.Type type) [0x00000] in <filename unknown>:0 
at System.Web.Mvc.DefaultControllerFactory+DefaultControllerActivator.Create (System.Web.Routing.RequestContext requestContext, System.Type controllerType) [0x00000] in <filename unknown>:0 
Version information: Mono Runtime Version: 2.10.9 (tarball Tue Mar 20 15:31:37 EDT 2012); ASP.NET Version: 4.0.30319.1
4

3 回答 3

0

或者您可以使用 ninject 的MVC 扩展,详见此处

我假设这将适用于 Mono

于 2012-04-03T11:09:19.753 回答
0

您可以添加默认构造函数

public HomeController()
{
}

但我认为,您错误地为控制器激活了 Ninject。您需要注册 ninject 工厂。确保您在 Global.asax 中的代码如下所示:

 public class MvcApplication : NinjectHttpApplication
    {
        /// <summary>
        /// Registers the global filters.
        /// </summary>
        /// <param name="filters">The filters.</param>
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        /// <summary>
        /// Registers the routes.
        /// </summary>
        /// <param name="routes">The routes.</param>
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional });
        }

        /// <summary>
        /// Creates the kernel that will manage your application.
        /// </summary>
        /// <returns>The created kernel.</returns>
        protected override IKernel CreateKernel()
        {
            var kernel = new StandardKernel();
            kernel.Load(Assembly.GetExecutingAssembly());
            return kernel;
        }

        /// <summary>
        /// Called when the application is started.
        /// </summary>
        protected override void OnApplicationStarted()
        {
            base.OnApplicationStarted();

            AreaRegistration.RegisterAllAreas();
            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }
    }

此外,对于示例,您可以在此处查看 MVC3 上的示例代码

于 2012-04-03T06:52:03.103 回答
0

我来晚了一点,但这个解决方案帮助了我。

覆盖默认构造函数:

public HomeController() : this(new Notifier())
{

}

public HomeController(INotifier notifier_)
{
    _notifier = notifier_;
}

于 2015-11-10T21:10:04.093 回答