Implementing dependency injection in a minimal API project
After understanding how to use dependency injection in an ASP.NET Core project, let’s try to understand how to use dependency injection in our minimal API project, starting with the default project using the WeatherForecast
endpoint.
This is the actual code of the WeatherForecast
GET endpoint:
app.MapGet("/weatherforecast", () => { var forecast = Enumerable.Range(1, 5).Select(index => new WeatherForecast ( DateTime.Now.AddDays(index), Random.Shared.Next(-20, 55), summaries[Random.Shared. Next(summaries.Length)] )) .ToArray(); return forecast; });
As we mentioned...