How can I access the query string from a self hosted MVC WebAPI?
Call to the following failed with NRE, because Current is empty (aka. null)
System.Web.HttpContext.Current.Request["myQuery"]
I need access to the current context outside of the controller, since I want to control my object instantiation via. DI. eg.
container
.RegisterType<MyObject>(
new InjectionFactory(
uc =>
new MyObject(
System.Web.HttpContext.Current.Request["myParam"]) //This failed.
));
Call to container.Resolve<MyObject>()
from inside the ControllerApi code failed, because of the above NRE.
HttpContext.Current isn't available in self hosted projects
see: HttpSelfHostServer and HttpContext.Current
You shouldn't really use
System.Web.HttpContext.Current
in Web API. It is only valid when using Web Host and is really only there for legacy reasons. Context information is tucked away in theHttpRequestMessage.Properties
collection.One of the ways that Web API improves testability is by removing its dependence on static properties.
There are ways to deal with resolving of instances and passing parameters. See this question Unity Application Block, How pass a parameter to Injection Factory?