未在无扩展页面中设置 HTTP 模块会话

发布于 2024-09-29 06:53:28 字数 252 浏览 1 评论 0原文

我编写了一个 HTTP 模块,需要访问会话。我已完成以下操作:

  • 模块在 web.config 中注册
  • 模块将我的方法调用附加到 PostAcquireRequestState 事件
  • 模块实现 IRequiresSessionState

但是,当我的页面没有扩展名时(即 htp://www.mywebsite.com )会话不可用,我的代码失败。如果页面确实有 aspx 扩展名,那么一切都可以。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

記柔刀 2024-10-06 06:53:28

您需要有一个由 ASP.NET 处理的项目,以便您的模块成为请求生命周期的一部分。提供像index.html 这样的页面无法实现这一点。 ASPX 页面将。

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.

巡山小妖精 2024-10-06 06:53:28

以下线程中的代码可以解决问题(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; }
        }
    }
}

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; }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文