Ninject.MVC3, Nuget, WebActivator oh my

前端 未结 2 1343
攒了一身酷
攒了一身酷 2021-02-01 23:41

I want to setup Ninject to do a simple test, as well as demonstrate the ease-of-setup using Nuget. I want to resolve a sample service.

public interface ITestSer         


        
2条回答
  •  离开以前
    2021-02-02 00:24

    Steps:

    1. Create a new ASP.NET MVC 3 project
    2. Install the NuGet package from the Package Console: Install-Package Ninject.MVC3
    3. In HomeController:

      public class HomeController : Controller
      {
          private readonly ITestService _service;
          public HomeController(ITestService service)
          {
              _service = service;
          }
      
          public ActionResult Index()
          {
              ViewBag.Message = _service.GetMessage();
              return View();
          }
      }
      
    4. In App_Start/NinjectMVC3.cs:

      private static void RegisterServices(IKernel kernel)
      {
          kernel.Bind().To();
      }       
      

提交回复
热议问题