I am exposing a view on a new API, and want to know how I can set a default value to a property, reading it from my appsettings.json file. How can I do it?
I've tried using dependency injection on the view, but it seems impossible since it's the framework that instantiate the class.
public class FilteringRequest
{
private readonly IAppContext appContext;
public FilteringRequest(IAppContext appContext)
{
this.appContext = appContext;
}
// TODO: appsettings?
// Perhaps something like appContext.DefaultType
public string Type { get; set; } = "top_rated";
// ...
}
This approach doesn't work since I believe the framework is expecting a parameterless default constructor.
An unhandled exception occurred while processing the request.
InvalidOperationException: Could not create an instance of type 'Wanderlust.API.Models.Requests.FilteringRequest'. Model bound complex types must not be abstract or value types and must have a parameterless constructor. Alternatively, give the 'request' parameter a non-null default value.
Microsoft.AspNetCore.Mvc.ModelBinding.Binders.ComplexTypeModelBinder.CreateModel(ModelBindingContext bindingContext)
Bear in mind that IAppContext already has all the properties defined in appsettings.json file.