简体   繁体   中英

ASP.NET MVC session per request pattern and IHttpModule

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?

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.

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

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.
     }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM