在您的自定義模塊的init中,您需要檢索會話模塊併爲Start事件添加事件處理程序。
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(Begin_Request);
IHttpModule sessionModule = context.Modules["Session"];
if(sessionModule != null &&
sessionModule.GetType() == typeof(System.Web.SessionState.SessionStateModule))
{
(sessionModule as System.Web.SessionState.SessionStateModule).Start
+= new EventHandler(CustomHttpModule_Start);
}
}
而且,我可以寫在Global.asax aspure C#,而不是使用標籤?
是,你可以在後面的在Global.asax添加代碼,並改變其內容爲
<%@ Application Language="C#" CodeBehind="Global.asax.cs" Inherits="Global" %>
後面的代碼應該System.Web.HttpApplication
public class Global : System.Web.HttpApplication
{
public Global() { }
void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
}
}
繼承