Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing httpcontext in class library with ASP.NET Core 1.1 (MVC)

I am working on migrating my ASP.NET 4.6 framework code over to .NET Core. I'm only in the learning phase at this point. I'm new to MVC as I've always used WebForms, but I want to move away from WebForms. I'm also new to ASP.NET Core.

Basically, I have a class library that does the bulk of my work - saving me from repeating code (database, session, custom identity management) across multiple pages or even websites. I add the DLL to a project, and it's ready to go.

I'm finding that ASPNET Core doesn't quite allow this. Perhaps I'm not understanding it, or perhaps it's too early to start porting over to it.

I need to be able to access HttpContext inside my class library to manipulate things like sessions, cookies, etc. What is the easiest way to approach this? I think once I can do that, I can start making better progress.

Or, do I need to reconsider writing a class library? From my understanding, I need to write Middleware to accomplish this. But every example I could find is either incomplete or outdated already.

Any suggestions are welcome.

like image 285
mackhax0r Avatar asked Apr 20 '17 19:04

mackhax0r


People also ask

How do I get HttpContext in .NET Core?

ASP.NET Core apps access HttpContext through the IHttpContextAccessor interface and its default implementation HttpContextAccessor. It's only necessary to use IHttpContextAccessor when you need access to the HttpContext inside a service.

How do I access HttpContext in Web API?

In ASP.NET Core, if we need to access the HttpContext in service, we can do so with the help of IHttpContextAccessor interface and its default implementation of HttpContextAccessor. It's only necessary to add this dependency if we want to access HttpContext in service.

What is HttpContext in ASP.NET MVC?

HttpContext is a type which has a static Current property that you're using to get the current context. There isn't a System. Web. Mvc.

What is HttpContext in ASP.NET Core?

The HttpContext object constructed by the ASP.NET Core web server acts as a container for a single request. It stores the request and response information, such as the properties of request, request-related services, and any data to/from the request or errors, if there are any.


2 Answers

Using HttpContext for all-around logic is not a great idea but if you really intend to do so, just stick to one of SOLID principles, ie. Dependency inversion principle.

ASP.NET MVC and it successor, ASP.NET Core have their foundations laid on this very principle. Inject IHttpContextAccessor in constructor of any class from your class library that needs it.


Example:

public class ContextHelper
{
    private IHttpContextAccessor _httpContextAccessor;

    public ContextHelper(IHttpContextAccessor httpContextAccessor)
    {
        _httpContextAccessor = httpContextAccessor;
    }

    public void DoStuff()
    {
        DoOtherStuffWith(_httpContextAccessor.HttpContext);
    }
}

Remember to register it in Startup in default IoC container like this:

public class Startup
{
    // Rest of the class here...

    public void ConfigureServices(IServiceCollection services)
    {
        // Other registrations here...

        services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
    }
}
like image 142
mwilczynski Avatar answered Sep 23 '22 06:09

mwilczynski


In asp.netcore mvc the HttpContext is made inaccessible outside the scope of controller. So, there is no way you can access HttpContext in your class library directly. You have to access it in some other ways.

You can send the HttpContext to your class library either from the constructor of your Controller to the constructor of your class library class or to the specific library method that needs the current HttpContext from the action method of the controller.

like image 39
Md Hasan Ibrahim Avatar answered Sep 20 '22 06:09

Md Hasan Ibrahim