2013-08-22 25 views
2

请参阅下面的代码如何处理IHttpModule中的会话?

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

+0

检查此其他帖子: [1]:http://stackoverflow.com/questions/276355/can-i-access-session-state-from-an-httpmodule –

回答

2

您正在请求BeginRequest中的会话状态,该状态在会话状态在应用程序生命周期中可用之前。至少,您不能使用会话状态,直到AcquireRequestState事件。

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

+0

你能帮我修改我想在我的代码中做 – Manu