想要在HttpHandler(ashx文件)中访问Session的状态值时,需要显式的实现一个接口 IReadOnlySessionState,示例如下:
<%
@ webhandler language
=
"
C#
"
class
=
"
DownloadHandler
"
%>
using System;
using System.Web;
using System.Web.SessionState ;
public class DownloadHandler : IHttpHandler, IReadOnlySessionState
{
public bool IsReusable { get { return true ; } }
public void ProcessRequest(HttpContext ctx)
{
ctx.Response.Write(ctx.Session[ " fred " ]);
}
}
using System;
using System.Web;
using System.Web.SessionState ;
public class DownloadHandler : IHttpHandler, IReadOnlySessionState
{
public bool IsReusable { get { return true ; } }
public void ProcessRequest(HttpContext ctx)
{
ctx.Response.Write(ctx.Session[ " fred " ]);
}
}
如果要读写Session的值,那么只要实现 IRequiresSessionState 接口就可以了,这两个接口没有待实现的方法,可直接使用。