Register .net MVC3 ControllerContext in the windsor container

With ASP.NET MVC3, what would be the best way to register ControllerContext requests in a lock windsor container? Ultimately, I would like to say

container.Resolve<ControllerContext>();

and returns a query from the query manager.

More details

The vast majority of my actions will simply perform verification, authentication, etc. before sending a message to nservicebus to actually do the job. To avoid having to copy / paste these 20/30 lines of code everywhere, I put them in a handler class in which my controllers depend on the constructor, the actions then call this class, which leaves my actions containing only one line of code.

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

+3
source share
2 answers

I don’t think you can register ControllerContext without any very ugly hacks, and IMHO is not a good idea. ControllerContext belongs to the controller, it is not intended for sharing.

However, if you only need 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);

ASP.NET MVC Windsor.Castle:. HttpContext-

, , ActionResults.

+1

, :) , ControllerContext - (, ) , . ModelBinders ActionAttributes - , . , , .

, , , , - Injection Dependency , IAuthenticationService IValidationService ( 20/30 ). ( MVC3 Global Filters, ).

0

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


All Articles