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?

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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

3 Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.