When using dependency injection we declare all dependencies within the constructor. But in ASP.NET MVC Controllers, I rarely see HttpContext
injected via constructor. Most probably this is due the fact it's conveniently available via static access in every Controller.
Now I thought it may be a good idea to also declare in the constructor of the HomeController
that we need HttpContext
(via IHttpContextAccessor
) to use this controller. Like this:
public class HomeController : Controller
{
protected new HttpContext HttpContext { get; }
public HomeController(IHttpContextAccessor httpContextAccessor)
{
HttpContext = httpContextAccessor.HttpContext;
}
}
Is this a common practice? Does it create major overhead? Or is it even a bad idea?