0%

Httpconetxt.Session==null的解决办法

在HttpHandler程序中使用Session时发现,HttpContext.Session==null ,后来在大神的博客得到了答案,原来需要IRequiresSessionState接口。

同时,自定义 HTTP 处理程序,从IHttpHandler继承,在写System.Web.HttpContext.Current.Session[“Value”]的时候,没有问题,但想将这个Session写到某个变量时或判断是否为空时如:HttpContext.Current.Session[“Value”]==null,发现Session的值为NULL,后来查MSDN,看到“在自定义 HTTP 处理程序中实现 IRequiresSessionState 接口,以确定处理程序是否需要对会话状态值具有读写访问权”,在自定义那个类上加上该接口后,果然正常了。

在使用asp.net编写webservice时,默认情况下是不支持session的,但我们可以把WebMethod的EnableSession选项设为true来显式的打开它,如下:

1
2
3
4
5
6
public class LogOut : IHttpHandler, IRequiresSessionState {
[WebMethod( EnableSession = true )] //需要using System.Web.Services;
public void ProcessRequest(HttpContext context)
{
}
}