December 2019
Intermediate to advanced
510 pages
11h 33m
English
A valid alternative to constructor injection is the action method injection. Sometimes, controllers use some dependencies in only one action method. In those cases, it may be useful to inject our dependency just in this action method, in order to improve the performance of our code. To perform an action method injection, we should use the [FromServices] attribute. For example, look at the following snippet of code:
using Microsoft.AspNetCore.Mvc;namespace SampleAPI.Controllers{ [ApiController] [Route("[controller]")] public class ValuesController : ControllerBase { [HttpGet] public ActionResult<string> Get( [FromServices]IPaymentService paymentService) { return paymentService.GetMessage(); } }}
The example, as aforementioned, ...
Read now
Unlock full access