Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpContext is null for MVC controller

I have a pretty vanilla controller:

public class HomeController : Controller
{
    private readonly ApplicationUserManager _applicationUserManager;

    public HomeController()
    {
        _applicationUserManager = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
    }
}

However, when I hit it the HttpContext it's null.

like image 342
BanksySan Avatar asked Oct 10 '15 19:10

BanksySan


People also ask

Is HttpContext ever null?

Clearly HttpContext. Current is not null only if you access it in a thread that handles incoming requests.

What is HttpContext MVC?

ASP.NET MVC 5 for Beginners 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.

Where is HttpContext items stored?

It is stored in the memory of the server and the value is available for the entire lifetime of the request. Save this answer.


1 Answers

HttpContext is being referenced in the constructor. There isn't an HttpContext here as they're only created when there is a request.

Moving HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>(); to an action solves the problem.

like image 153
BanksySan Avatar answered Sep 29 '22 01:09

BanksySan