开发者

ASP.NET MVC Injecting Http/Request/Controller context

开发者 https://www.devze.com 2023-02-20 04:03 出处:网络
Is there a recommended approach for injecting Http/Request/Controller context in an ASP.NET MVC application?

Is there a recommended approach for injecting Http/Request/Controller context in an ASP.NET MVC application?

Previously I've o开发者_如何学运维nly 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


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:

For<RequestContext>().Use(ctx => HttpContext.Current.Request.RequestContext);
For<RouteCollection>().Use(ctx => RouteTable.Routes);
0

精彩评论

暂无评论...
验证码 换一张
取 消