ASP.NET MVC 注入 Http/Request/Controller 上下文
是否有推荐的方法在 ASP.NET MVC 应用程序中注入 Http/Request/Controller 上下文?
以前我只使用 HttpContext 完成此操作,如下所示(我使用的是 StructureMap):
For<HttpContextBase>().Use(ctx => new HttpContextWrapper(HttpContext.Current));
但是,在某些情况下,我还需要访问请求上下文。与其手动构建它,不如将其注入。一个很好的例子是注入 UrlHelper(需要 RequestContext 和 RouteCollection)。
谢谢
本
Is there a recommended approach for injecting Http/Request/Controller context in an ASP.NET MVC application?
Previously I've only done this with HttpContext like so (I'm using StructureMap):
For<HttpContextBase>().Use(ctx => new HttpContextWrapper(HttpContext.Current));
However, in some cases I also need to get access to the request context. Rather than building this manually, it would be nice to have it injected. A good example would be injecting a UrlHelper (requires RequestContext and RouteCollection).
Thanks
Ben
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能需要考虑是否真的想直接依赖这些上下文对象(它们往往会使依赖于它们的东西难以测试)。也就是说,您走在正确的道路上:
You might want to consider whether you really want to depend on those context objects directly (they tend to make things that depend on them hard to test). That said, you are on the right track: