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.

januszstabik
  • 1,152
  • 5
  • 16
  • 30

3 Answers3

0

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
  • 382
  • 2
  • 7
  • 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 Oct 26 '10 at 14:12
  • Is it possible that the page is being cached by the browser? – Pedro Oct 26 '10 at 14:14
  • No, i can step through the request and evaluate the request at every step. – januszstabik Oct 26 '10 at 15:48
0

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; }
        }
    }
}
Community
  • 1
  • 1
Nariman
  • 6,368
  • 1
  • 35
  • 50
  • 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 Oct 27 '10 at 08:07
  • Which web server are you seeing this on? – Nariman Oct 27 '10 at 11:32
-2

It turns out its an II7 issue, see here:

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

januszstabik
  • 1,152
  • 5
  • 16
  • 30