2

请看下面的代码

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用于进行自定义 URL 重定向。但是在目标页面中设置会话时显示错误。

错误行代码:

HttpContext.Current.Session[USERADMIN] == null

错误信息:

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

4

1 回答 1

2

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

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

于 2013-08-22T13:29:41.027 回答