Uchenna

InvalidOperationException: Could not create an instance of type 'Microsoft.AspNetCore.Hosting.IWebHostEnvironment'. Model bound complex types must not be abstract or value types and must have a parameterless constructor. Record types must have a single primary constructor. Alternatively, give the 'environment' parameter a non-null default value.

net Icon

System.InvalidOperationException

Could not create an instance of type 'Microsoft.AspNetCore.Hosting.IWebHostEnvironment'. Model bound complex types must not be abstract or value types and must have a parameterless constructor. Record types must have a single primary constructor. Alternatively, give the 'environment' parameter a non-null default value.

An unhandled exception occurred
Exception source not available.

Solution

You can inject a dependency to action methods from the parameter, but you must mark it with the [FromServices] attribute.

https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/dependency-injection?view=aspnetcore-5.0#action-injection-with-fromservices            
Mar 29, 2021 Anyone

0 replies


Install the browser extension

Found this solution helpful? Try our extension for quick solutions to your .NET errors.

Chrome logo Add to Chrome - It's free