Session为空的一种原因

在维护一份比较老的代码,想改为ajax调用,然后就添加了一个一般处理程序文件,也就是以.ashx结尾的文件,一切都正常,但发现session一直为空,很奇怪


基本的代码如下:

 

public class GetDataSurveyPerformance : IHttpHandler
{

    private string OperationTypeList = "list";
    private string OperationTypeAdd = "add";
    private string OperationTypeDel = "del";

    public void ProcessRequest(HttpContext context)
    {
         HttpSessionState session = HttpContext.Current.Session;//为空,根本取不到存在Session中的数据
		 
        string sourceurl2 = context.Request.UrlReferrer.LocalPath.ToString().ToLower().Trim();
        string type = context.Request.QueryString["OperationType"].ToLower().Trim();
        if (sourceurl2.EndsWith("SurveyPerformanceList.aspx".ToLower().Trim()))
        {
            SurveyPerformanceAction(context);
        }
        else if (sourceurl2.EndsWith("SurveyPerformanceadd.aspx".ToLower().Trim()))
        {
         SurveyPerformanceAction(context);
        }
    }
}


最后发现,在其他页面都可以取到session中保存的内容,只有此处为空,很奇怪。经过很长时间的测试,解决方法竟然也很简单,就是还需要继承一个接口IRequiresSessionState

 

也就是改为如下代码就可以解决了:

 

public class GetDataSurveyPerformance : IHttpHandler, IRequiresSessionState//多了一个接口IRequiresSessionState
{

    private string OperationTypeList = "list";
    private string OperationTypeAdd = "add";
    private string OperationTypeDel = "del";

    public void ProcessRequest(HttpContext context)
    {
         HttpSessionState session = HttpContext.Current.Session;//现在不为空了,能取到存在Session中的数据
		 
        string sourceurl2 = context.Request.UrlReferrer.LocalPath.ToString().ToLower().Trim();
        string type = context.Request.QueryString["OperationType"].ToLower().Trim();
        if (sourceurl2.EndsWith("SurveyPerformanceList.aspx".ToLower().Trim()))
        {
            SurveyPerformanceAction(context);
        }
        else if (sourceurl2.EndsWith("SurveyPerformanceadd.aspx".ToLower().Trim()))
        {
         SurveyPerformanceAction(context);
        }
    }
}


 


 

posted @   jlins  阅读(4039)  评论(2编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
点击右上角即可分享
微信分享提示