DI into Controllers

Constructor Injection

public class HomeController : Controller
{
    private readonly IDateTime _dateTime;

    public HomeController(IDateTime dateTime)
    {
        _dateTime = dateTime;
    }

		// ... controllers can use _dateTime
}

Action Injection

public IActionResult About([FromServices] IDateTime dateTime)
{
    return Content( $"Current server time: {dateTime.Now}");
}

Action Injection from a Keyed Service

public ActionResult<object> GetSmallCache([FromKeyedServices("small")] ICache cache)
{
    return cache.Get("data-mvc");
}