开发者

HTTP module session not being set in extensionless page

开发者 https://www.devze.com 2023-01-22 13:45 出处:网络
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

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 al开发者_JS百科l is ok.


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.


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


It turns out its an II7 issue, see here:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消