HTTP module session is not installed on page without extension

I have an HTTP module that I wrote that should access a session. I have done the following:

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

However, when my page does not have an extension (for example, like htp: //www.mywebsite.com), the session is unavailable and my code does not work. If the page has an aspx extension, that's fine.

+3
source share
3 answers

, ASP.NET, . , index.html, . ASPX .

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

Source: https://habr.com/ru/post/1771496/


All Articles