Alw*_*wyn 5 c# asp.net-mvc asp.net-mvc-4 asp.net-web-api
如何从自托管的MVC WebAPI访问查询字符串?
使用NRE调用以下失败,因为Current为空(aka.null)
System.Web.HttpContext.Current.Request["myQuery"]
Run Code Online (Sandbox Code Playgroud)
我需要访问控制器外部的当前上下文,因为我想通过控制我的对象实例化.DI.例如.
container
.RegisterType<MyObject>(
new InjectionFactory(
uc =>
new MyObject(
System.Web.HttpContext.Current.Request["myParam"]) //This failed.
));
Run Code Online (Sandbox Code Playgroud)
container.Resolve<MyObject>()由于上述NRE,从ControllerApi代码内部调用失败.
Dar*_*ler 11
您不应该真正使用System.Web.HttpContext.CurrentWeb API.它仅在使用Web Host时才有效,并且仅用于遗留原因.上下文信息隐藏在HttpRequestMessage.Properties集合中.
Web API提高可测试性的方法之一是消除其对静态属性的依赖.
有办法处理实例的解析和传递参数.看到这个问题Unity Application Block,如何将参数传递给Injection Factory?
| 归档时间: |
|
| 查看次数: |
21065 次 |
| 最近记录: |