I am doing an MVC 5 with API2 Controller Application.
I have a Controller that call an ApiController in the same project. In my Api Controller Method I want to use DI.
It looks like this.
[ActionName("Method1")]
[HttpPost]
public int Method1(long User_id)
{
long user_id = Newtonsoft.Json.JsonConvert.DeserializeObject<long>(User_id.ToString());
IContactRepository _contactRepository = DependencyResolver.Current.GetService<IContactRepository>();
return _contactRepository.Get_CountMensajeByUser(user_id);
}
In order to use
[ActionName("RetProduct")] and [HttpPost]
I have to include using System.Web.Http; reference
But, in the other hand, in order to use DependencyResolver I have to include
using System.Web.Mvc;
when I include that reference, System.Web.Http; does not work.
Another alternative is to set de DI in the API Controller Constructor like this
private IContactRepository _sessionRepository;
public ApiController(IContactRepository contactRepository)
{
_contactRepository = contactRepository;
}
So i avoid to use DependencyResolver. But I have to use Parameters in the Controller Constructor.
On my controller where I call Api controller like this.
var webApi = new APIController();
model.CantMensajeByUser = webApi.CountMensajeByUser(10);
Where I am not using parameters.
What is the correct way to do it?