1

我想在某些页面中检查 Session。为此,我将添加要在 web.config 中检查的页面名称作为appsetting键。我想在发现会话为空或其他内容后使用 httpHandler 触发事件。

如果我将 httpHandler 创建为 dll(另一个项目)并添加到网站,处理程序可以触发事件并且网站可以在网页中捕获它吗?

4

1 回答 1

0

你可以做的是:

您的 HttpHandler 在集合中放置一个值,HttpContext.Current.Items告诉您是否存在 Session。就像是

HttpContext.Current.Items.Add("SessionWasThere") = true;

您创建一个 BasePage 来检查Page_Load事件中的值并引发一个新事件,告诉您:

public abstract class BasePage : Page {
    public event EventHandler NoSession;

    protected override void OnLoad(EventArgs e){  
        var sessionWasThere = (bool)HttpContext.Current.Items.Add("SessionWasThere");
        if(!sessionWasThere && NoSession != null)
            NoSession(this, EventArgs.Empty);
    }
}

在您的页面中,您订阅了该事件:

public class MyPage : BasePage{

    protected override void OnInit(){
        NoSession += Page_NoSession;
    }

    private void Page_NoSession(object sender, EventArgs e) {
        //...
    }
}
于 2012-08-13T13:26:40.113 回答