0

I have the following code in a controller which, when executed, displays the view to the user in the browser:

public class PlayerController : Controller

{
    private readonly IPlayerRepository repo;
    public IActionResult Index()
    {
        return View();
    }
    [HttpPost]
    public ActionResult Index(Player person)
    {
        var lastName = person.LastName;
        var firstName = person.FirstName;
        var callPlayer = new PlayerInit();
        var returnPlayer = callPlayer.SetUpPlayer(lastName, firstName);

        return View();
    }
}

When I add this code, an exception is thrown:

    public PlayerController(IPlayerRepository repo)
    {
        this.repo = repo;
    }

This is the exception that gets thrown:

enter image description here

I have similar code in a different controller and I am able to run that controller to access a mySql database via dapper and display the results to the view. Im not sure why the code in this particular controller is having an issue.

Thank you!

1
  • 2
    The error is clear and has nothing to do with databases. Have you registered any IPlayerRepository-derived class? What does your Startup.cs look like? Commented Nov 26, 2021 at 16:23

1 Answer 1

2

Usually this error is caused because in the Startup.cs file the ConfigureServices method does not contain the code that will inject the dependency to the container.

public void ConfigureServices(IServiceCollection services)
{
    // OMITTED

    services.AddScoped<IPlayerRepository, PlayerRepository>();

    // OMITTED
}

You can also take a look to the documentation on how all the dependency injection works in dotnet starting here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.