1
Answer

Inject dependency into Controller.

Ask a question
Hi
I am trying inject dependency into my controller but object of IService is getting null,
i have written an code as below
 
public interface IService
{
string GetData(string data);
}
 
public class Service:IService
{
   public string GetData(string data)
   {
       return " new data:"+data;
   } 
 
my controller
public class HomeController:Controller
{
 private IService _service;
   public HomeController(IService service)
   {
      _service=service;
   }
 
public ActionResult Index()
  string data=_service.GetData("new in MVC");  //here i am getting NullValueException.
 return View();
 
and object _service  is getting null.
so please let me know how to fixed this problem....
  
 
 

Answers (1)