Take the 2-minute tour ×

I'm trying to make a solution like in HttpModule with ASP.NET MVC not being called

How do I filter the request? I only want to open an ISession if the request is for an ASP.NET MVC action, not for *.gif, *.css, etc.

How should I handle this filtering?

share|improve this question

3 Answers 3

up vote 2 down vote accepted

Sessions are very cheap to create, I wouldn't bother with this filter.

Literally, opening a ISession is just a matter of a new SessionImpl(..). SessionImpl constructor and dispose don't do much if nothing happened in the session.

share|improve this answer

You could add the managedHandler precondition to your module. But I don't know how well it'll fit in with ASP.NET MVC because of static files passing though routing.

Anyway, you could try something like:

<add name="RequestTimer" type="MySite.HttpModule.RequestTimeModule, MySite" precondition="managedHandler" />

Have a look here for more information - IIS7 Preconditions


share|improve this answer
Well Maybe I should just make an ActionFilter and add it to each controller. But I like the idea of an HttpModule –  Rasmus Christensen Jan 10 '10 at 0:22
Did you try putting in the precondition? What exactly is it for? Could you go about tackling it another way? –  Charlino Jan 10 '10 at 5:40
No I didn't try the precondition. I will just make another solution. As Mauricio describes below ISession is cheap in creating, and if you don't hit the db no connection is made after all. Well I still dont't like to create objects I will not use. I will just make UnitOfWork in another way I guess, Might use some actionfilter –  Rasmus Christensen Jan 10 '10 at 15:51

You can use this:

void IHttpModule.Init(HttpApplication context)
    context.PreRequestHandlerExecute += new System.EventHandler(context_PreRequestHandlerExecute);

And then you can check if it is the MVC handler (type MvcHandler) which will execute your request:

 void context_PreRequestHandlerExecute(object sender, System.EventArgs e)
     HttpContext context = ((HttpApplication)sender).Context;
     Type mvcht = typeof(System.Web.Mvc.MvcHandler);
     if (context.Handler != null && context.Handler.GetType().IsAssignableFrom(mvcht))
         ..... Code goes here.
share|improve this answer

Your Answer


