When we try to access SPContext object in HttpApplication.BeginRequest event in custom httpmodule for SharePoint site, we will get an error like SPContext is null.
In order to overcome this issue, move your code to PreRequestHandlerExecute event.
Sample Code: public class MyCustmHTTPModule : IHttpModule
{
public void Init(HttpApplication appObj)
{
appObj.BeginRequest += new EventHandler(Application_BeginRequest);
appObj.PreRequestHandlerExecute += new EventHandler(Application_PreRequestHandlerExecute);
}
public void Application_BeginRequest(object sender, EventArgs e)
{
//SPContext is null here
}
public void Application_PreRequestHandlerExecute(object sender, EventArgs e)
{
//SPContext can be accessed here
SPUser currentUser = SPContext.Current.Web.CurrentUser;
}
}
|