日期:2014-05-18  浏览次数:20399 次

会话状态在此上下文中不可用 HttpModule无法拿到Sesson
C# code
 

        public void Init(HttpApplication a)
        {
            a.AcquireRequestState += new EventHandler(a_BeginRequest);
         
        }

        void a_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication a = (HttpApplication)sender;
            HttpSessionState s = (HttpSessionState)a.Context.Session;
            if (s["user"] == null)
            {
                a.Context.Response.Redirect(@"~\Forms\backstage\login.html");
                --在这里如果通过重定向过去页面就报Sesson没将对象设置到实例..
                --但是我通过server派遣过去的话就能显示 由于页面路径不一样 Login.html的样式
                --图片全部加载不了 ,求高手指点(如果在同一目录派遣都会报上面的那个错误!)
            }
         
        }


        public void Dispose()
        {
            throw new Exception("The method or operation is not implemented.");
        }}


上面代码是这样写的 理论没错...问题就是报错了

------解决方案--------------------
public class TestModule : IHttpModule
{
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.AcquireRequestState += (o, e) =>
{
HttpApplication app = (HttpApplication)o;
if (app.Context.Handler.ToString().EndsWith("aspx"))
{
app.Session["Test"] = "1";
app.Response.Write(app.Session["Test"].ToString());
}
};
}
}