0

This is the situation, I'm using a Razor page to create an ASP.NET Core 6 MVC site for a friend, the thing is I'm trying to inject a controller into another controller and I'm getting an error while building the project.

Here is a screenshot showing the error:

enter image description here

I made sure to have the interface class added in Program.cs (I used AddTransient<,>() however it didn't worked with scoped or singleton either, I'm not sure of the difference between them).

So now I'm wondering if controllers are not supposed to be instantiated via DI.

If you guys need any extra information I'm more than pleased to share it!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 2
    _" I'm trying to inject a controller into another controller "_ - but why? =) – Guru Stron Nov 09 '22 at 18:54
  • because I had the folders structured as an API, and I didn't want to use the MVC Controller as the "Main" controller, I mean, I can just simplily delete the "main" controller and use the MVC one as the main. – Celalyvan Nov 09 '22 at 21:00
  • I think you can try to put the methods which you want into the services,and then use [depedency injection](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-7.0). – Yiyi You Nov 10 '22 at 07:04
  • Yeahm but that would turn my non MVC Controller useless :( – Celalyvan Nov 10 '22 at 12:58
  • Yes,but usually we will not inject one controller to another.If you still want to do it,you can refer to the [link](https://stackoverflow.com/questions/34963586/asp-net-core-call-a-controller-from-another-controller). – Yiyi You Nov 11 '22 at 09:48
  • Thanks! I'll look into it! However if you are telling me that that's not what you would normally do, I think I'll just call the service layer from the MVC controller – Celalyvan Nov 12 '22 at 15:29

0 Answers0