views:

284

answers:

3

Hi

Im trying to make a solution like this http://stackoverflow.com/questions/1391060/httpmodule-with-asp-net-mvc-not-being-called Now the question is how do I filter the request, I only want to Open an ISession if the request is for an ASPNET MVC Action, not for *.gif, *.css etc.

How should I handle this filtering?

+1  A: 

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
Well Maybe I should just make an ActionFilter and add it to each controller. But I like the idea of an HttpModule
Rasmus Christensen
Did you try putting in the precondition? What exactly is it for? Could you go about tackling it another way?
Charlino
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
A: 

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
A: 

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 MvcHandler the one who 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
}
}