views:

33

answers:

3

I have a HTTP module that I have written that needs to access the session. I have done the following:

  • Module is registered in web.config
  • Module attaches my method call to the PostAcquireRequestState event
  • The module implement IRequiresSessionState

However, when my page doesn't have an extension (i.e. as when htp://www.mywebsite.com) the session is not available and my code fails. If the page does have an aspx extension then all is ok.

A: 

You need to have an item that is processed by ASP.NET in order for your module to be part of the request life-cycle. Serving a page like index.html won't accomplish that. An ASPX page will.

Pedro
I do though. The page that is getting loaded by the root is default.aspx. If I request default.aspx specifically then the session is available, however if I just use http://www.mywebsite.com then it is not?
januszstabik
Is it possible that the page is being cached by the browser?
Pedro
No, i can step through the request and evaluate the request at every step.
januszstabik
A: 

The code from the following thread does the trick (1):

public class Module : IHttpModule, IRequiresSessionState
{
    public void Dispose()
    {
    }

    void OnPostMapRequestHandler(object source, EventArgs e)
    {
        HttpApplication app = (HttpApplication)source;

        if (app.Context.Handler is IReadOnlySessionState || app.Context.Handler is IRequiresSessionState)
            return;

        app.Context.Handler = new MyHttpHandler(app.Context.Handler);
    }

    void OnPostAcquireRequestState(object source, EventArgs e)
    {
        HttpApplication app = (HttpApplication)source;

        MyHttpHandler resourceHttpHandler = HttpContext.Current.Handler as MyHttpHandler;

        if (resourceHttpHandler != null)
            HttpContext.Current.Handler = resourceHttpHandler.OriginalHandler;
    }

    public void Init(HttpApplication httpApp)
    {
        httpApp.PostAcquireRequestState += new EventHandler(OnPostAcquireRequestState);
        httpApp.PostMapRequestHandler += new EventHandler(OnPostMapRequestHandler);
    }

    public class MyHttpHandler : IHttpHandler, IRequiresSessionState
    {
        internal readonly IHttpHandler OriginalHandler;

        public void ProcessRequest(HttpContext context)
        {
            throw new InvalidOperationException("MyHttpHandler cannot process requests.");
        }

        public MyHttpHandler(IHttpHandler originalHandler)
        {
            OriginalHandler = originalHandler;
        }

        public bool IsReusable
        {
            get { return false; }
        }
    }
}
Nariman
HI there, unfortunately this doesn't work. I'd already tried it but to no avail. Even after swapping handler there is no session state available when requesting an extensionless page. Is there anything else that could be causing this? I'm running this on the Umrbaco source code which already has a few http modules installed, however, none of them use the session?
januszstabik
Which web server are you seeing this on?
Nariman
A: 

It turns out its an II7 issue, see here:

http://forum.umbraco.org/yaf_postst9997_ContextSession-always-null-on-top-umbraco-page.aspx

januszstabik