将.net MVC3 ControllerContext注册到windsor容器中

发布于 2024-10-08 09:22:01 字数 413 浏览 0 评论 0原文

使用 ASP.NET MVC3 将请求 ControllerContext 注册到温莎城堡容器中的最佳方法是什么?最终我希望能够说出

container.Resolve<ControllerContext>();

并返回请求控制器上下文。

更多细节

我的绝大多数操作只是在向 nservicebus 发送消息以实际完成工作之前进行一些验证、身份验证等。为了避免必须在各处复制/粘贴这 20/30 行代码,我将它们放入一个处理程序类中,我的控制器在构造函数中依赖该处理程序类,然后操作调用此类,这使得我的操作仅包含一个行代码。

组成处理程序的子类之一需要了解所采取的路线,我可以将控制器传递给处理程序,然后传递给此类,但这似乎有点混乱。如果有办法让温莎注册并为我提供它,那就太好了。

With ASP.NET MVC3 what would be the best way to register the requests ControllerContext into castle windsor container? Ultimately I'd like to be able to say

container.Resolve<ControllerContext>();

and have the requests controller context returned.

More detail

The vast majority of my actions are just going to do some validation, authentication, etc.. before sending a message to nservicebus to actually do the work. To avoid having to copy/paste these 20/30 lines of code all over the place I have put them into a handler class which my controllers take a dependency on in the constructor, the actions then call this class which leaves my actions containing just one line of code.

One of the child classes that makes up the handler needs to know about the route that was taken, I could just pass the controller to the handler and then onto this class but it seems a bit messy. It would be nice if there was a way to get Windsor registered to provide it for me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扎心 2024-10-15 09:22:01

我不认为你可以在没有一些非常丑陋的黑客的情况下注册 ControllerContext,而且恕我直言,无论如何这都不是一个好主意。 ControllerContext 属于控制器,它不应该被共享。

但是,如果您只需要路由信息,则可以像这样注册它(未经测试!):

container.Register(Component.For<HttpContextBase>()
                    .UsingFactoryMethod(() => new HttpContextBaseWrapper(HttpContext.Current))
                    .LifeStyle.PerWebRequest,
                   Component.For<RouteData>()
                    .UsingFactoryMethod(k => RouteTable.Routes.GetRouteData(k.Resolve<HttpContextBase>()))
                    .LifeStyle.PerWebRequest);

另请参阅 ASP.NET MVC 和Windsor.Castle:使用 HttpContext 相关服务了解更多详细信息。

我具体不知道你想要实现什么,但我会考虑使用过滤器或自定义 ActionResults 来实现它。

I don't think you can register the ControllerContext without some very ugly hacks, and IMHO it's not a good idea anyway. The ControllerContext belongs to the controller, it's not meant to be shared around.

However, if you only need the routing information, you can register it like this (UNTESTED!):

container.Register(Component.For<HttpContextBase>()
                    .UsingFactoryMethod(() => new HttpContextBaseWrapper(HttpContext.Current))
                    .LifeStyle.PerWebRequest,
                   Component.For<RouteData>()
                    .UsingFactoryMethod(k => RouteTable.Routes.GetRouteData(k.Resolve<HttpContextBase>()))
                    .LifeStyle.PerWebRequest);

Also see ASP.NET MVC & Windsor.Castle: working with HttpContext-dependent services for more details.

I don't know concretely what you're trying to achieve but I'd look into doing it with filters or custom ActionResults instead.

烂人 2024-10-15 09:22:01

这并不是对原始问题的真正答案,但我无法忍受可疑的架构移动:)对我来说,ControllerContext 并不是一个最好的(好吧,甚至不是一个好的)尝试扩展的地方。 ModelBinders 和 ActionAttributes 是帮助重复代码的地方。他们可以自行进行模型映射、绑定和验证,从而将控制器从该责任中解放出来。

好吧,一般来说,您真正想做的是对控制器本身使用依赖注入,注入一些 IAuthenticationServiceIValidationService 的配置实例(它们的具体实现将包含所有那些 20/30 可重用的代码行)。然后在控制器中,您只需用一行代码调用它们(甚至使用 MVC3 全局过滤器功能使其完全透明)。

Not really an answer to the original question but I can't stand dubious architectural moves :) It seems for me like ControllerContext isn't a best (well, not even a good one) place to try to extend from. The ModelBinders and ActionAttributes are the places to help with repeating code. They can take model mapping, binding and validation on their own thus freeing controllers from that responsibility.

Well, generally, what you'd really want to do is to use Dependency Injection for Controllers themselves injecting configured instances of somewhat IAuthenticationService and IValidationService (concrete implementations of them would contain all those 20/30 reusable lines of code). Then in Controllers you just call them in one line of code (or even use the MVC3 Global Filters feature to make that completely transparent).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文