BanksySan
BanksySan

Reputation: 28500

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.

Upvotes: 8

Views: 8052

Answers (1)

BanksySan
BanksySan

Reputation: 28500

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.

Upvotes: 19

Related Questions