Home > database >  Which dependencies can be injected through action methods in ASP.NET Core?
Which dependencies can be injected through action methods in ASP.NET Core?

Time:06-17

I wondered how I could get the form data from a cshtml to the controller. I tried HttpContext.Request.Form.Data and this didn't work. By accident I found a question on SO where someone used:

public IActionResult Validate(IFormCollection form)

This worked. Now I wonder how I can look up what other dependencies can be injected this way. Either by looking up the debugger, documentation or the source code. So in the future I know where to search.

CodePudding user response:

Any method which is called during startup may be adding many services to the DI container so it would be hard to document such a thing.

The easiest way, like you said is to use the debugger.

In .NET 5, you could hit a break-point in the ConfigureServices Method (in Startup.cs) and inspect the "services" argument as below. Expanding Results View will show many, (as you can see there are now 274, but before the call to AddRazorPages, there was less than 100.

enter image description here

For .NET6, you can just hit a break-point in Program.cs and inspect Builder.Services as shown below

enter image description here

  • Related