在自托管WebApi中获取HttpRequest上下文
如何从自托管的MVC WebAPI访问查询字符串?
使用NRE调用以下失败,因为Current为空(aka。null)
System.Web.HttpContext.Current.Request["myQuery"]
我需要访问控制器外部的当前上下文,因为我想通过控制我的对象实例化。 DI。 例如。
container .RegisterType( new InjectionFactory( uc => new MyObject( System.Web.HttpContext.Current.Request["myParam"]) //This failed. ));
由于上面的NRE,从ControllerApi代码内部调用container.Resolve()
失败。
您不应该在Web API中使用System.Web.HttpContext.Current
。 它仅在使用Web Host时才有效,并且仅用于遗留原因。 上下文信息隐藏在HttpRequestMessage.Properties
集合中。
Web API提高可测试性的方法之一是消除其对静态属性的依赖。
有办法处理实例的解析和传递参数。 看到这个问题Unity Application Block,如何将参数传递给Injection Factory?
HttpContext.Current在自托管项目中不可用
请参阅: HttpSelfHostServer和HttpContext.Current
- MVC 4 – LogOff控制器操作,找不到404
- 来自数据库的MVC模型validation
- 在ASP.NET MVC 4 C#Code First中指定ON DELETE NO ACTION
- Asp:net MVC 3:@ Html.Editor在模板中对我的模型进行子集合?
- 使用AzureReader2插件的Azure ImageResizer没有resize
- C#MVC 4:将View中的JavaScript数组传递给Controller
- 如何成功使用existingResponse =“Auto”?
- Custom MembershipProvider:没有为此对象定义的无参数构造函数
- dependency injection初始化