我是建筑师的新手。请解释为什么它会给我以下错误:
"An error occurred when trying to create a controller of type 'HomeController'. Make sure that the controller has a parameterless public constructor.
这里我有1个接口(IRepo)1类文件(回购) IRepo.cs
public interface IRepo
{
IEnumerable<Employee> GetEmployee();
IQueryable<Employee> GetEmployee(int id);
}
Repo.cs
Ctxdb _db = null;
public Repo(Ctxdb db)
{
this._db = db;
}
public IEnumerable<Employee> GetEmployee()
{
//
}
HomeController.cs
IRepo _ObjRepo = null;
public HomeController(Repo ObjRepo)
{
_ObjRepo = ObjRepo;
}
[Route("GetEmp")]
[HttpGet]
public IHttpActionResult GetDat()
{
var x = _ObjRepo.GetEmployee();
if (x != null)
return Content(HttpStatusCode.OK, x);
else
return Content(HttpStatusCode.BadRequest,"Not Implemented");
}
答案 0 :(得分:5)
看起来你不想实现依赖注入。如果那是正确的,你应该做三件事:
IRepo
代替Repo
official docs中有一个很棒的教程。