HttpContext.Current.Session is null in Ashx file

橙三吉。 提交于 2019-11-26 09:28:30

问题


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.

Use example:

Session = HttpContext.Current.Session // This is null

Or:

public virtual void ProcessRequest(HttpContext context)
{
    System.Web.SessionState.HttpSessionState Session = context.Session; 
    // This is null
}

In the Web.config:

<sessionState timeout=\"1800\"></sessionState>

回答1:


You must add on your handler the IRequiresSessionState on the declaration of it as:

public class YourHandleName : IHttpHandler, 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.



来源:https://stackoverflow.com/questions/14181408/httpcontext-current-session-is-null-in-ashx-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!