HttpContext.Current.Session 在 Ashx 文件中为 null
问题描述:
我看到了一些问题(这里和此处)但他们没有回答我的问题.我正在尝试使用ajax.ashx"文件调用 Ajax,并在函数中访问会话.出于某种原因,Session 对象本身的值为 null.
I saw some questions (Here and Here) but they do not answer my question. I am trying to call Ajax using "ajax.ashx" file, and in function to access Session. For some reason, the value of the Session object itself is null.
使用示例:
Session = HttpContext.Current.Session // This is null
或者:
public virtual void ProcessRequest(HttpContext context)
{
System.Web.SessionState.HttpSessionState Session = context.Session;
// This is null
}
在 Web.config 中:
In the Web.config:
<sessionState timeout="1800"></sessionState>
答
您必须在处理程序中添加 IRequiresSessionState
声明为:
You must add on your handler the IRequiresSessionState
on the declaration of it as:
public class YourHandleName : IHttpHandler, IRequiresSessionState
{
...
默认情况下,处理程序不与会话连接以保持最小,通过添加 IRequiresSessionState
将它们附加到会话中.
by default the handlers are not connected with the session to keep them minimum, by adding the IRequiresSessionState
you attach them with the session.