Using HttpContextAccessor in a .NET 4.6 Project

I created a .NET Core project (class library), which is also for .NET 4.6, which should have access to the current HTTP context. I see that we can no longer use the static HttpContext.Current and must introduce an instance of IHttpContextAccessor . Is this something that I can still use inside something like a .NET 4.6 web API project? So far, I cannot get HttpContextAccessor.HttpContext to return anything but null.

+5
source share
1 answer

For a class library, I think you can pass the corresponding variables / objects through the constructor or methods. This is good practice because your class library will not be broken if you reference it in a console application without httpcontext, for example.

If you use a controller method, you can simply use a request or response.

+1
source

All Articles