无法在不抛出错误的情况下将接口传递给控制器

Can't pass in interface to controller without throwing error

我创建了一个服务 class 'TeacherService' 带有一个接口,我想将它传递到我的控制器的构造函数中,但是当我尝试时,其中一个构造函数出现错误

只要我在构造函数参数中输入一个逗号,它就会变成红色的下划线

new TeacherRepository()

说它无法解析构造函数。 这是代码。

public class TeacherController : Controller
{
    private readonly ITeacherRepository teacherRepository;
    private  readonly ITeacherService teacherService;

    // If you are using Dependency Injection, you can delete the following constructor
    public TeacherController() : this(new TeacherRepository())
    {
    }

    public TeacherController(ITeacherRepository teacherRepository, ITeacherService teacherService)
    {
        this.teacherRepository = teacherRepository;
        this.teacherService = teacherService;
    }
public TeacherController() : this(new TeacherRepository())

您正在调用第二个构造函数,它只需要一个参数就需要两个参数。添加默认的 TeacherService 让它编译。