I know that controllers are supposed to handle every action but how do I preprocess some requests in asp .net mvc? For example there are a lot of things that are required in all my controllers. Coding this in each controller isn’t quite right so I need to save some data in the http context session so that I can access it in every controller.
And if I store the objects in the session I also need to somehow properly dispose or make available for later use some objects in the httpcontext session? In my case, I use nhibernate to map my objects to a database and I was thinking of creating a pool where to store my ISession instances,sessions or connections with my database, and make them available as required. I don’t know if it’s okay like this but it’s the best idea I came up with rather than creating those ISession everytime.
I’m pretty new to this so I probably use a lot of these things not so well.
Override the
MvcHandlerclass and implement your logic there. Then override theMvcRouteHandlerto provide your customIHttpHandlerimplementation instead of the defaultMvcHandler. This will be easier to do if you look at the source of these classes and understand exactly what is happening in between ASP.NET receiving the request and the controller executing.Another option is to create a
BaseControllerthat handles all the preprocessing, and derive any controllers in the solution from the base controller. This would work, but it doesn’t seem as appropriate for what you are trying to do as the first solution I suggested.