简体   繁体   中英

No parameterless constructor defined for this object. MVC 5

I have a problem when i try to code with .NET Framework(4.7.2) and EF Core(3.0) because usually i use .NET Core. When i try to inject Repository(by its interface).

This is my code

SenderController.cs

public class SenderController : Controller
{
    private readonly ISenderRepository _sRepo;
    public SenderController(ISenderRepository sRepo)
    {
        _sRepo = sRepo;
    }

    public ActionResult Index()
    {
        return View();
    }

ISenderRepository.cs

 public interface ISenderRepository
{
    ICollection<Sender> GetSenders();
    Sender GetSender(int senderId);
    bool SenderExists(string name);
    bool SenderExists(int id);
    bool CreateSender(Sender sender);
    bool UpdateSender(Sender sender);
    bool DeleteSender(Sender sender);
    bool Save();
}

and this is what i get

在此处输入图片说明

i think i do wrong way for dependency injection in MVC 5. Can you help me to do that? Thank you :)

I just fixing it by using this statement in constructor :

private ISenderRepository _sRepo;
    public SenderController()
    {
        this._sRepo = new SenderRepository(new Data.ApplicationDbContext());
    }

Thanks

确保你必须注入你的控制器,你可以使用 Autofac 来帮助你

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM