强制的HttpHandler使用SessionState会HttpHandler、SessionState

2023-09-06 18:46:46 作者:只能牵我的手

我想自定义自动售卖产品,线路通过HttpHandler的所有请求。处理程序分析用户找出哪些页面路由请求并执行Server.Transfer的()。不幸的是,我需要访问SessionState会在页面上的处理程序没有实现IRequiresSessionState并被标记为内部的,所以我不能继承它。经过大量的使用Google我找到了最好的解决方案是创建一个的HttpModule 的变化该处理在请求中的生命周期中的不同点的请求的处理程序。在波斯特马prequestHandler我想改变这种处理实现IRequiresSessionState和PostAcquireRequestState我将映射回要求我自己的处理程序。

I am trying to customize a vended product that routes all requests through a HttpHandler. The handler analyzes the request to figure out what page to route the user to and performs a Server.Transfer(). Unfortunately, I need to access SessionState on a page and the handler doesn't implement IRequiresSessionState and is marked as internal so I can't inherit from it. After a lot of googling the best solution I found was to create an HttpModule that changes the handler that processes the request at different points in the request lifecycle. On PostMapRequestHandler I would change the handler that processes the request to my own that implements IRequiresSessionState and PostAcquireRequestState I would map it back.

这工作,但没有任何人有一个更好的解决方案?

This works but does anyone have a better solution?

推荐答案

我想出了一个更优雅的方式来实现会话状态。您可以覆盖一个处理程序的会话状态的行为。我创建了一个强制会话状态的模块。

I figured out a more elegant way to enable session state. You can override the session state behavior of a handler. I created a module that forces session state.

public class SessionEnablerModule : IHttpModule
{
    public void Dispose()
    {

    }

    public void Init(HttpApplication context)
    {
        context.PostMapRequestHandler += new EventHandler(context_PostMapRequestHandler);
    }

    void context_PostMapRequestHandler(object sender, EventArgs e)
    {
        HttpApplication app = (HttpApplication)sender;

        if ((app.Context.Handler.GetType().ToString().Equals("Handler I want to enable session state for")))
        {
            //enable session state
            app.Context.SetSessionStateBehavior(SessionStateBehavior.Required);
        }
    }
}