Posts

Showing posts with the label How to access HttpContext in ASP.NET Core?

How to access HttpContext in ASP.NET Core?

HttpContext.Current  doesn't exist anymore in ASP.NET Core but there's a new  IHttpContextAccessor  that you can inject in your dependencies and use to retrieve the current  HttpContext : public class MyComponent : IMyComponent { private readonly IHttpContextAccessor _contextAccessor; public MyComponent ( IHttpContextAccessor contextAccessor ) { _contextAccessor = contextAccessor; } public string GetDataFromSession () { return _contextAccessor.HttpContext.Session.GetString(*KEY*); } }