获取InRequestScope对象MVC过滤器 [英] Getting InRequestScope object in MVC Filters

查看:165
本文介绍了获取InRequestScope对象MVC过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知ASP MVC缓存器 - >所以他们没有在这样的要求范围内绑定,如果我会问ninject里面的对象的实例,我会得到它完全地新天地

As far as i know ASP MVC caches filters -> so they are not bound in request scope thus if i will ask ninject inside it for an instance of object i will get it in completly new scope.

在网络的API我可以使用actionContext.Request.GetDependencyScope(),其中ActionContext中:: HttpActionContext从请求范围检索对象的实例。我一直在谷歌上搜索了一段时间,我无法找到MVC类似的事情。所以主要是qustion ::有没有什么方法来检索势必请求范围MVC过滤器里面的对象实例?

In Web-Api I can use actionContext.Request.GetDependencyScope() where actionContext :: HttpActionContext to retrieve objects instances from request scope. I have been googling for some time and I cannot find anything similar in MVC. So the main qustion is :: Is there any way to retrieve object instances bound in request scope inside MVC filters?

推荐答案

我看了一下Ninject.web.mvcX实施 InRequestScope()
它采用 HttpContext.Current 的范围对象。

I've had a look at the implementation of Ninject.web.mvcX InRequestScope(). It uses HttpContext.Current as scope object.

这意味着,如果你已经有了

That means, if you've got

Bind<IFoo>().To<Foo>().InRequestScope();

您可以使用注入

IResolutionRoot.Get&LT;的IFoo&GT;()

基本上任何地方/随时随地在该 HttpContext.Current 是有效。

basically anywhere/anytime at which HttpContext.Current is "valid".

要绑定的过滤器,可以使用ninject.web.mvcX的NuGet包,并做到:

To bind the filter, you can use the ninject.web.mvcX nuget package and do:

BindFilter<FooFilter>(FilterScope.Controller, 0)

这允许你使用构造函数注入,过滤器。另请参见 Ninject维基:为过滤器依赖注入

This allows you to use ctor-injection with the filter. Also see Ninject Wiki: Dependency Injection for Filters

我不太确定这是否改变时/频率过滤器实例化,但我不这么认为。因此,为了访问 InRequestScope()对象过滤器,你就必须注入一个 IResolutionRoot 或 - 甚至更好 - 工厂(请参见 ninject.extensions.factory )到过滤器来创建对象/对象图。

I'm not quite sure whether this changes when / how often filters are instanciated, but i don't think so. So in order to access an InRequestScope() object in your filter, you'll have to inject an IResolutionRoot or - even better - a factory (see ninject.extensions.factory) into your filter to create the object / object graph.

这篇关于获取InRequestScope对象MVC过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆