如何在IHttpModule中处理Session?

时间:2013-08-22 13:16:51

标签: c# asp.net

请参阅以下代码

public class URLRewriter : IHttpModule {

    public void Dispose() {

    }
    public void Init( HttpApplication context ) {
        context.BeginRequest += new EventHandler( context_BeginRequest );
    }

    void context_BeginRequest( object sender, EventArgs e ) {
                //code to make custom 
                 URLhttpApplication.Context.Server.Transfer( CustomPath );

        }
}

我在这里使用IHttpModule进行自定义网址重定向。但是在目标页面中设置会话时显示错误。

错误行代码:

HttpContext.Current.Session[USERADMIN] == null

错误消息:

  

System.NullReferenceException:对象引用未设置为   对象的实例。

1 个答案:

答案 0 :(得分:2)

您要求BeginRequest中的会话状态,这是在会话状态在应用程序的生命周期中可用之前。至少,您不能在AcquireRequestState事件之前使用会话状态。

更改您的Init以处理AcquireRequestState而不是BeginRequest。