1

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?

Community
  • 1
  • 1
Rasmus Christensen
  • 8,321
  • 12
  • 51
  • 78

3 Answers3

2

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.

Mauricio Scheffer
  • 98,863
  • 23
  • 192
  • 275
1

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

HTHs,
Charles

Charlino
  • 15,802
  • 3
  • 58
  • 74
  • 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 00: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 05: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
0

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.
     }
 }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131