如何在使用依赖项注入时调用我的Web API控制器

时间:2014-06-30 19:49:35

标签: dependency-injection asp.net-web-api2

我想通过web api控制器将所有调用路由到数据库。问题是我使用ninject依赖注入服务到我的web api控制器。因此,当我的MVC控制器需要调用web api控制器时,我不能只是实例化它的新实例,或者我必须将我使用的所有注入服务传递给MVC控制器,然后将它们作为参数重新注入到api控制器:< / p>

 UserController : Controller
 {
   {
    private readonly IGroupService _groupService;

    public UserController(IGroupService groupService)
    {
        _groupService = groupService;
    }

     public ActionResult Index()
     {
         var model = new UserModel();
         model.Group = _groupService.get();
         //I don't want to do this
     }
  }

   public class UserApiController : ApiController
{
    private readonly IUserService _userService;
    private readonly IStatusConverter _statusConverter;

    public UserApiController(IUserService userService, IStatusConverter statusConverter)
    {
        _userService = userService;
        _statusConverter = statusConverter;
    }

    public IEnumerable<WebUser> Get()
    {
        return _userService.Get();
    }

我无法在MVC控制器中实例化新的ApiController,而无需向mvc控制器添加未使用的注入。有什么建议吗?

0 个答案:

没有答案