ASP.NET MVC Injecting Http / Request / Controller context

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

I used to do this only with HttpContext (I use StructureMap):

For<HttpContextBase>().Use(ctx => new HttpContextWrapper(HttpContext.Current)); 

However, in some cases, I also need to access the request context. Instead of building it manually, it would be nice if it were introduced. A good example would be an UrlHelper injection (RequestContext and RouteCollection required).

thanks

Ben

+4
source share
1 answer

Perhaps you should consider whether you really want to directly depend on these context objects (they usually do things that depend on them, it's hard to test). However, you are on the right track:

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

Source: https://habr.com/ru/post/1345347/


All Articles