I am trying to access the RequestContext in the constructor of my WebAPI Controller. I have created a base WebAPI Controller from which all of my other WebAPI Controllers derive.
I tried originally accessing the Request.Headers properties directly, but these values always came up as null.
Therefore, I ended up doing this instead:
public BaseApiController() { IEnumerable<string> headerValues = new List<string>(); Debug.WriteLine(_userNameHeader); if (System.Web.HttpContext.Current.Request.Headers.Get(_userNameHeader) != null) { //Check if the header values exists as part of the Request headerValues = System.Web.HttpContext.Current.Request.Headers.GetValues(_userNameHeader); //The username should be the 1st (and only) element in the header _userName = headerValues.FirstOrDefault(); }//if }
However, I would like to use Ninject to use the appropriate HttpControllerContext to inject this into the constructor so that I can easily Unit Test my Web API Controllers using Moq.
The current way I have this set up does not allow me to Unit Test any of my Controllers because I end up getting a null object reference.
Please advise as to how to resolve this issue.
Thanks.