ASP.NET:如何从处理程序访问会话?
在重定向到WebForms页面之前,我会选择会话值并预填充WebForm:ASP.NET:如何从处理程序访问会话?,asp.net,session,session-state,handler,ihttphandler,Asp.net,Session,Session State,Handler,Ihttphandler,在重定向到WebForms页面之前,我会选择会话值并预填充WebForm: public class Handler : IHttpHandler { public void ProcessRequest(HttpContext context) { ... context.Session["StackOverflow"] = "overflowing"; context.Response.Redirect("~/AnotherPage.aspx"
public class Handler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
...
context.Session["StackOverflow"] = "overflowing";
context.Response.Redirect("~/AnotherPage.aspx");
...
}
...
}
上下文除外。会话对象为空
如何从处理程序访问会话状态?实现接口
public class Handler : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
context.Session["StackOverflow"] = "overflowing";
context.Response.Redirect("~/AnotherPage.aspx");
}
}
实施
IRequiresessionState
实施是否解决了这一问题
改为执行IHttpModule并重写BeginRequest怎么样
public void Init(HttpApplication application)
{
application.BeginRequest += new EventHandler(context_BeginRequest);
}
注意:您不必实际实现任何东西,只需将接口添加到类中即可。然后,web服务器会看到您在请求它,并填写它。是的,它仍在实现接口,但由于它是一个标记接口,因此除了接口的派生之外,我们不需要编写任何代码。出于某种原因,即使指定了
irequiresessionstate
,我的代码也无法工作。我不得不使用IReadOnlySessionState
。我还没有研究原因,但它正在发挥作用。有人知道哪一个性能更好吗?我面临的问题与我在处理程序代码中使用IRequiresessionState时遇到的问题相同,但我仍然无法访问会话值。会话密钥仍然为0。有人能告诉我这件事吗?