When building ASP.NET Core applications, accessing HttpContext in a controller's constructor or in a DI-injected service is not straightforward. The solution is to use IHttpContextAccessor: register it in Startup.cs via services.AddHttpContextAccessor(), then inject it into the constructor of any controller or service that needs request data like the Host header. This gives full access to the HttpRequest at construction time.

2m read timeFrom nodogmablog.bryanhogan.net
Post cover image