0

Is it possible to access System.Web.HttpContext.Current in a ASP.NET core project targeting the .NET framework . It currently returns null .

Is using Microsoft.AspNetCore.Mvc.HttpContext the only way to go about it ?

user581157
  • 1,327
  • 4
  • 26
  • 64

1 Answers1

1

you can use the IHttpContextAccessor helper service to get the HTTP context in any class that is managed by the ASP.NET Core dependency injection system. This is useful when you have a common service that is used by your controllers.

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

you can then access the current HTTP context in a safe way:

var context = _httpContextAccessor.HttpContext;

if this is what your asking for.

ZIADIA Oussama
  • 151
  • 2
  • 13
  • Thanks for your answers . I meant more on the lines of mimicking System.Web.HttpContext . I found this article which exaplins that .https://www.strathweb.com/2016/12/accessing-httpcontext-outside-of-framework-components-in-asp-net-core/ – user581157 Jan 31 '19 at 09:43
  • Are you actually in a situation where you need to sub in this functionality while you make updates to an existing library, as the article details? If not, *do not go down this path*. Even if you *are*, you should have an eye on removing this as soon as possible. This is not something you just add and then say "Great! I'll now use this forever." – Chris Pratt Jan 31 '19 at 13:55