【问题标题】:How to handle Session in IHttpModule?如何处理 IHttpModule 中的 Session?
【发布时间】:2013-08-22 13:16:50
【问题描述】:

请看下面的代码

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:对象引用未设置为 对象的实例。

【问题讨论】:

标签: c# asp.net


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-23
    相关资源
    最近更新 更多